88#include <linux/clk.h>
99#include <linux/init.h>
1010#include <linux/platform_device.h>
11+ #include <linux/spi/spi.h>
1112
1213#include <asm/io.h>
1314
@@ -310,8 +311,6 @@ static void genclk_mode(struct clk *clk, int enabled)
310311{
311312 u32 control ;
312313
313- BUG_ON (clk -> index > 7 );
314-
315314 control = sm_readl (& system_manager , PM_GCCTRL + 4 * clk -> index );
316315 if (enabled )
317316 control |= SM_BIT (CEN );
@@ -325,11 +324,6 @@ static unsigned long genclk_get_rate(struct clk *clk)
325324 u32 control ;
326325 unsigned long div = 1 ;
327326
328- BUG_ON (clk -> index > 7 );
329-
330- if (!clk -> parent )
331- return 0 ;
332-
333327 control = sm_readl (& system_manager , PM_GCCTRL + 4 * clk -> index );
334328 if (control & SM_BIT (DIVEN ))
335329 div = 2 * (SM_BFEXT (DIV , control ) + 1 );
@@ -342,11 +336,6 @@ static long genclk_set_rate(struct clk *clk, unsigned long rate, int apply)
342336 u32 control ;
343337 unsigned long parent_rate , actual_rate , div ;
344338
345- BUG_ON (clk -> index > 7 );
346-
347- if (!clk -> parent )
348- return 0 ;
349-
350339 parent_rate = clk -> parent -> get_rate (clk -> parent );
351340 control = sm_readl (& system_manager , PM_GCCTRL + 4 * clk -> index );
352341
@@ -373,11 +362,8 @@ int genclk_set_parent(struct clk *clk, struct clk *parent)
373362{
374363 u32 control ;
375364
376- BUG_ON (clk -> index > 7 );
377-
378365 printk ("clk %s: new parent %s (was %s)\n" ,
379- clk -> name , parent -> name ,
380- clk -> parent ? clk -> parent -> name : "(null)" );
366+ clk -> name , parent -> name , clk -> parent -> name );
381367
382368 control = sm_readl (& system_manager , PM_GCCTRL + 4 * clk -> index );
383369
@@ -399,6 +385,22 @@ int genclk_set_parent(struct clk *clk, struct clk *parent)
399385 return 0 ;
400386}
401387
388+ static void __init genclk_init_parent (struct clk * clk )
389+ {
390+ u32 control ;
391+ struct clk * parent ;
392+
393+ BUG_ON (clk -> index > 7 );
394+
395+ control = sm_readl (& system_manager , PM_GCCTRL + 4 * clk -> index );
396+ if (control & SM_BIT (OSCSEL ))
397+ parent = (control & SM_BIT (PLLSEL )) ? & pll1 : & osc1 ;
398+ else
399+ parent = (control & SM_BIT (PLLSEL )) ? & pll0 : & osc0 ;
400+
401+ clk -> parent = parent ;
402+ }
403+
402404/* --------------------------------------------------------------------
403405 * System peripherals
404406 * -------------------------------------------------------------------- */
@@ -750,8 +752,41 @@ static struct resource atmel_spi1_resource[] = {
750752DEFINE_DEV (atmel_spi , 1 );
751753DEV_CLK (spi_clk , atmel_spi1 , pba , 1 );
752754
753- struct platform_device * __init at32_add_device_spi (unsigned int id )
755+ static void
756+ at32_spi_setup_slaves (unsigned int bus_num , struct spi_board_info * b ,
757+ unsigned int n , const u8 * pins )
758+ {
759+ unsigned int pin , mode ;
760+
761+ for (; n ; n -- , b ++ ) {
762+ b -> bus_num = bus_num ;
763+ if (b -> chip_select >= 4 )
764+ continue ;
765+ pin = (unsigned )b -> controller_data ;
766+ if (!pin ) {
767+ pin = pins [b -> chip_select ];
768+ b -> controller_data = (void * )pin ;
769+ }
770+ mode = AT32_GPIOF_OUTPUT ;
771+ if (!(b -> mode & SPI_CS_HIGH ))
772+ mode |= AT32_GPIOF_HIGH ;
773+ at32_select_gpio (pin , mode );
774+ }
775+ }
776+
777+ struct platform_device * __init
778+ at32_add_device_spi (unsigned int id , struct spi_board_info * b , unsigned int n )
754779{
780+ /*
781+ * Manage the chipselects as GPIOs, normally using the same pins
782+ * the SPI controller expects; but boards can use other pins.
783+ */
784+ static u8 __initdata spi0_pins [] =
785+ { GPIO_PIN_PA (3 ), GPIO_PIN_PA (4 ),
786+ GPIO_PIN_PA (5 ), GPIO_PIN_PA (20 ), };
787+ static u8 __initdata spi1_pins [] =
788+ { GPIO_PIN_PB (2 ), GPIO_PIN_PB (3 ),
789+ GPIO_PIN_PB (4 ), GPIO_PIN_PA (27 ), };
755790 struct platform_device * pdev ;
756791
757792 switch (id ) {
@@ -760,35 +795,22 @@ struct platform_device *__init at32_add_device_spi(unsigned int id)
760795 select_peripheral (PA (0 ), PERIPH_A , 0 ); /* MISO */
761796 select_peripheral (PA (1 ), PERIPH_A , 0 ); /* MOSI */
762797 select_peripheral (PA (2 ), PERIPH_A , 0 ); /* SCK */
763-
764- /* NPCS[2:0] */
765- at32_select_gpio (GPIO_PIN_PA (3 ),
766- AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH );
767- at32_select_gpio (GPIO_PIN_PA (4 ),
768- AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH );
769- at32_select_gpio (GPIO_PIN_PA (5 ),
770- AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH );
798+ at32_spi_setup_slaves (0 , b , n , spi0_pins );
771799 break ;
772800
773801 case 1 :
774802 pdev = & atmel_spi1_device ;
775803 select_peripheral (PB (0 ), PERIPH_B , 0 ); /* MISO */
776804 select_peripheral (PB (1 ), PERIPH_B , 0 ); /* MOSI */
777805 select_peripheral (PB (5 ), PERIPH_B , 0 ); /* SCK */
778-
779- /* NPCS[2:0] */
780- at32_select_gpio (GPIO_PIN_PB (2 ),
781- AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH );
782- at32_select_gpio (GPIO_PIN_PB (3 ),
783- AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH );
784- at32_select_gpio (GPIO_PIN_PB (4 ),
785- AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH );
806+ at32_spi_setup_slaves (1 , b , n , spi1_pins );
786807 break ;
787808
788809 default :
789810 return NULL ;
790811 }
791812
813+ spi_register_board_info (b , n );
792814 platform_device_register (pdev );
793815 return pdev ;
794816}
@@ -872,6 +894,50 @@ at32_add_device_lcdc(unsigned int id, struct lcdc_platform_data *data)
872894 return pdev ;
873895}
874896
897+ /* --------------------------------------------------------------------
898+ * GCLK
899+ * -------------------------------------------------------------------- */
900+ static struct clk gclk0 = {
901+ .name = "gclk0" ,
902+ .mode = genclk_mode ,
903+ .get_rate = genclk_get_rate ,
904+ .set_rate = genclk_set_rate ,
905+ .set_parent = genclk_set_parent ,
906+ .index = 0 ,
907+ };
908+ static struct clk gclk1 = {
909+ .name = "gclk1" ,
910+ .mode = genclk_mode ,
911+ .get_rate = genclk_get_rate ,
912+ .set_rate = genclk_set_rate ,
913+ .set_parent = genclk_set_parent ,
914+ .index = 1 ,
915+ };
916+ static struct clk gclk2 = {
917+ .name = "gclk2" ,
918+ .mode = genclk_mode ,
919+ .get_rate = genclk_get_rate ,
920+ .set_rate = genclk_set_rate ,
921+ .set_parent = genclk_set_parent ,
922+ .index = 2 ,
923+ };
924+ static struct clk gclk3 = {
925+ .name = "gclk3" ,
926+ .mode = genclk_mode ,
927+ .get_rate = genclk_get_rate ,
928+ .set_rate = genclk_set_rate ,
929+ .set_parent = genclk_set_parent ,
930+ .index = 3 ,
931+ };
932+ static struct clk gclk4 = {
933+ .name = "gclk4" ,
934+ .mode = genclk_mode ,
935+ .get_rate = genclk_get_rate ,
936+ .set_rate = genclk_set_rate ,
937+ .set_parent = genclk_set_parent ,
938+ .index = 4 ,
939+ };
940+
875941struct clk * at32_clock_list [] = {
876942 & osc32k ,
877943 & osc0 ,
@@ -908,6 +974,11 @@ struct clk *at32_clock_list[] = {
908974 & atmel_spi1_spi_clk ,
909975 & lcdc0_hclk ,
910976 & lcdc0_pixclk ,
977+ & gclk0 ,
978+ & gclk1 ,
979+ & gclk2 ,
980+ & gclk3 ,
981+ & gclk4 ,
911982};
912983unsigned int at32_nr_clocks = ARRAY_SIZE (at32_clock_list );
913984
@@ -936,6 +1007,13 @@ void __init at32_clock_init(void)
9361007 if (sm_readl (sm , PM_PLL1 ) & SM_BIT (PLLOSC ))
9371008 pll1 .parent = & osc1 ;
9381009
1010+ genclk_init_parent (& gclk0 );
1011+ genclk_init_parent (& gclk1 );
1012+ genclk_init_parent (& gclk2 );
1013+ genclk_init_parent (& gclk3 );
1014+ genclk_init_parent (& gclk4 );
1015+ genclk_init_parent (& lcdc0_pixclk );
1016+
9391017 /*
9401018 * Turn on all clocks that have at least one user already, and
9411019 * turn off everything else. We only do this for module
0 commit comments