spi-s3c64xx.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. /*
  2. * Copyright (C) 2009 Samsung Electronics Ltd.
  3. * Jaswinder Singh <jassi.brar@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/clk.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/gpio.h>
  31. #include <linux/of.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/platform_data/spi-s3c64xx.h>
  34. #ifdef CONFIG_S3C_DMA
  35. #include <mach/dma.h>
  36. #endif
  37. #define MAX_SPI_PORTS 3
  38. #define S3C64XX_SPI_QUIRK_POLL (1 << 0)
  39. /* Registers and bit-fields */
  40. #define S3C64XX_SPI_CH_CFG 0x00
  41. #define S3C64XX_SPI_CLK_CFG 0x04
  42. #define S3C64XX_SPI_MODE_CFG 0x08
  43. #define S3C64XX_SPI_SLAVE_SEL 0x0C
  44. #define S3C64XX_SPI_INT_EN 0x10
  45. #define S3C64XX_SPI_STATUS 0x14
  46. #define S3C64XX_SPI_TX_DATA 0x18
  47. #define S3C64XX_SPI_RX_DATA 0x1C
  48. #define S3C64XX_SPI_PACKET_CNT 0x20
  49. #define S3C64XX_SPI_PENDING_CLR 0x24
  50. #define S3C64XX_SPI_SWAP_CFG 0x28
  51. #define S3C64XX_SPI_FB_CLK 0x2C
  52. #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
  53. #define S3C64XX_SPI_CH_SW_RST (1<<5)
  54. #define S3C64XX_SPI_CH_SLAVE (1<<4)
  55. #define S3C64XX_SPI_CPOL_L (1<<3)
  56. #define S3C64XX_SPI_CPHA_B (1<<2)
  57. #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
  58. #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
  59. #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
  60. #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
  61. #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
  62. #define S3C64XX_SPI_PSR_MASK 0xff
  63. #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
  64. #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
  65. #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
  66. #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
  67. #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
  68. #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
  69. #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
  70. #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
  71. #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
  72. #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
  73. #define S3C64XX_SPI_MODE_4BURST (1<<0)
  74. #define S3C64XX_SPI_SLAVE_AUTO (1<<1)
  75. #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
  76. #define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
  77. #define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
  78. #define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
  79. #define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
  80. #define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
  81. #define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
  82. #define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
  83. #define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
  84. #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
  85. #define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
  86. #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
  87. #define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
  88. #define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
  89. #define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
  90. #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
  91. #define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
  92. #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
  93. #define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
  94. #define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
  95. #define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
  96. #define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
  97. #define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
  98. #define S3C64XX_SPI_SWAP_RX_EN (1<<4)
  99. #define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
  100. #define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
  101. #define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
  102. #define S3C64XX_SPI_SWAP_TX_EN (1<<0)
  103. #define S3C64XX_SPI_FBCLK_MSK (3<<0)
  104. #define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
  105. #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
  106. (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
  107. #define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
  108. #define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
  109. FIFO_LVL_MASK(i))
  110. #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
  111. #define S3C64XX_SPI_TRAILCNT_OFF 19
  112. #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
  113. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  114. #define is_polling(x) (x->port_conf->quirks & S3C64XX_SPI_QUIRK_POLL)
  115. #define RXBUSY (1<<2)
  116. #define TXBUSY (1<<3)
  117. struct s3c64xx_spi_dma_data {
  118. struct dma_chan *ch;
  119. enum dma_transfer_direction direction;
  120. unsigned int dmach;
  121. };
  122. /**
  123. * struct s3c64xx_spi_info - SPI Controller hardware info
  124. * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
  125. * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
  126. * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
  127. * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
  128. * @clk_from_cmu: True, if the controller does not include a clock mux and
  129. * prescaler unit.
  130. *
  131. * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
  132. * differ in some aspects such as the size of the fifo and spi bus clock
  133. * setup. Such differences are specified to the driver using this structure
  134. * which is provided as driver data to the driver.
  135. */
  136. struct s3c64xx_spi_port_config {
  137. int fifo_lvl_mask[MAX_SPI_PORTS];
  138. int rx_lvl_offset;
  139. int tx_st_done;
  140. int quirks;
  141. bool high_speed;
  142. bool clk_from_cmu;
  143. };
  144. /**
  145. * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
  146. * @clk: Pointer to the spi clock.
  147. * @src_clk: Pointer to the clock used to generate SPI signals.
  148. * @master: Pointer to the SPI Protocol master.
  149. * @cntrlr_info: Platform specific data for the controller this driver manages.
  150. * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
  151. * @queue: To log SPI xfer requests.
  152. * @lock: Controller specific lock.
  153. * @state: Set of FLAGS to indicate status.
  154. * @rx_dmach: Controller's DMA channel for Rx.
  155. * @tx_dmach: Controller's DMA channel for Tx.
  156. * @sfr_start: BUS address of SPI controller regs.
  157. * @regs: Pointer to ioremap'ed controller registers.
  158. * @irq: interrupt
  159. * @xfer_completion: To indicate completion of xfer task.
  160. * @cur_mode: Stores the active configuration of the controller.
  161. * @cur_bpw: Stores the active bits per word settings.
  162. * @cur_speed: Stores the active xfer clock speed.
  163. */
  164. struct s3c64xx_spi_driver_data {
  165. void __iomem *regs;
  166. struct clk *clk;
  167. struct clk *src_clk;
  168. struct platform_device *pdev;
  169. struct spi_master *master;
  170. struct s3c64xx_spi_info *cntrlr_info;
  171. struct spi_device *tgl_spi;
  172. struct list_head queue;
  173. spinlock_t lock;
  174. unsigned long sfr_start;
  175. struct completion xfer_completion;
  176. unsigned state;
  177. unsigned cur_mode, cur_bpw;
  178. unsigned cur_speed;
  179. struct s3c64xx_spi_dma_data rx_dma;
  180. struct s3c64xx_spi_dma_data tx_dma;
  181. #ifdef CONFIG_S3C_DMA
  182. struct samsung_dma_ops *ops;
  183. #endif
  184. struct s3c64xx_spi_port_config *port_conf;
  185. unsigned int port_id;
  186. unsigned long gpios[4];
  187. bool cs_gpio;
  188. };
  189. static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
  190. {
  191. void __iomem *regs = sdd->regs;
  192. unsigned long loops;
  193. u32 val;
  194. writel(0, regs + S3C64XX_SPI_PACKET_CNT);
  195. val = readl(regs + S3C64XX_SPI_CH_CFG);
  196. val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
  197. writel(val, regs + S3C64XX_SPI_CH_CFG);
  198. val = readl(regs + S3C64XX_SPI_CH_CFG);
  199. val |= S3C64XX_SPI_CH_SW_RST;
  200. val &= ~S3C64XX_SPI_CH_HS_EN;
  201. writel(val, regs + S3C64XX_SPI_CH_CFG);
  202. /* Flush TxFIFO*/
  203. loops = msecs_to_loops(1);
  204. do {
  205. val = readl(regs + S3C64XX_SPI_STATUS);
  206. } while (TX_FIFO_LVL(val, sdd) && loops--);
  207. if (loops == 0)
  208. dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
  209. /* Flush RxFIFO*/
  210. loops = msecs_to_loops(1);
  211. do {
  212. val = readl(regs + S3C64XX_SPI_STATUS);
  213. if (RX_FIFO_LVL(val, sdd))
  214. readl(regs + S3C64XX_SPI_RX_DATA);
  215. else
  216. break;
  217. } while (loops--);
  218. if (loops == 0)
  219. dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
  220. val = readl(regs + S3C64XX_SPI_CH_CFG);
  221. val &= ~S3C64XX_SPI_CH_SW_RST;
  222. writel(val, regs + S3C64XX_SPI_CH_CFG);
  223. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  224. val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
  225. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  226. }
  227. static void s3c64xx_spi_dmacb(void *data)
  228. {
  229. struct s3c64xx_spi_driver_data *sdd;
  230. struct s3c64xx_spi_dma_data *dma = data;
  231. unsigned long flags;
  232. if (dma->direction == DMA_DEV_TO_MEM)
  233. sdd = container_of(data,
  234. struct s3c64xx_spi_driver_data, rx_dma);
  235. else
  236. sdd = container_of(data,
  237. struct s3c64xx_spi_driver_data, tx_dma);
  238. spin_lock_irqsave(&sdd->lock, flags);
  239. if (dma->direction == DMA_DEV_TO_MEM) {
  240. sdd->state &= ~RXBUSY;
  241. if (!(sdd->state & TXBUSY))
  242. complete(&sdd->xfer_completion);
  243. } else {
  244. sdd->state &= ~TXBUSY;
  245. if (!(sdd->state & RXBUSY))
  246. complete(&sdd->xfer_completion);
  247. }
  248. spin_unlock_irqrestore(&sdd->lock, flags);
  249. }
  250. #ifdef CONFIG_S3C_DMA
  251. /* FIXME: remove this section once arch/arm/mach-s3c64xx uses dmaengine */
  252. static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
  253. .name = "samsung-spi-dma",
  254. };
  255. static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
  256. unsigned len, dma_addr_t buf)
  257. {
  258. struct s3c64xx_spi_driver_data *sdd;
  259. struct samsung_dma_prep info;
  260. struct samsung_dma_config config;
  261. if (dma->direction == DMA_DEV_TO_MEM) {
  262. sdd = container_of((void *)dma,
  263. struct s3c64xx_spi_driver_data, rx_dma);
  264. config.direction = sdd->rx_dma.direction;
  265. config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
  266. config.width = sdd->cur_bpw / 8;
  267. sdd->ops->config((enum dma_ch)sdd->rx_dma.ch, &config);
  268. } else {
  269. sdd = container_of((void *)dma,
  270. struct s3c64xx_spi_driver_data, tx_dma);
  271. config.direction = sdd->tx_dma.direction;
  272. config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
  273. config.width = sdd->cur_bpw / 8;
  274. sdd->ops->config((enum dma_ch)sdd->tx_dma.ch, &config);
  275. }
  276. info.cap = DMA_SLAVE;
  277. info.len = len;
  278. info.fp = s3c64xx_spi_dmacb;
  279. info.fp_param = dma;
  280. info.direction = dma->direction;
  281. info.buf = buf;
  282. sdd->ops->prepare((enum dma_ch)dma->ch, &info);
  283. sdd->ops->trigger((enum dma_ch)dma->ch);
  284. }
  285. static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
  286. {
  287. struct samsung_dma_req req;
  288. struct device *dev = &sdd->pdev->dev;
  289. sdd->ops = samsung_dma_get_ops();
  290. req.cap = DMA_SLAVE;
  291. req.client = &s3c64xx_spi_dma_client;
  292. sdd->rx_dma.ch = (void *)sdd->ops->request(sdd->rx_dma.dmach, &req, dev, "rx");
  293. sdd->tx_dma.ch = (void *)sdd->ops->request(sdd->tx_dma.dmach, &req, dev, "tx");
  294. return 1;
  295. }
  296. static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
  297. {
  298. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  299. /*
  300. * If DMA resource was not available during
  301. * probe, no need to continue with dma requests
  302. * else Acquire DMA channels
  303. */
  304. while (!is_polling(sdd) && !acquire_dma(sdd))
  305. usleep_range(10000, 11000);
  306. pm_runtime_get_sync(&sdd->pdev->dev);
  307. return 0;
  308. }
  309. static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
  310. {
  311. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  312. /* Free DMA channels */
  313. if (!is_polling(sdd)) {
  314. sdd->ops->release((enum dma_ch)sdd->rx_dma.ch,
  315. &s3c64xx_spi_dma_client);
  316. sdd->ops->release((enum dma_ch)sdd->tx_dma.ch,
  317. &s3c64xx_spi_dma_client);
  318. }
  319. pm_runtime_put(&sdd->pdev->dev);
  320. return 0;
  321. }
  322. static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
  323. struct s3c64xx_spi_dma_data *dma)
  324. {
  325. sdd->ops->stop((enum dma_ch)dma->ch);
  326. }
  327. #else
  328. static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
  329. unsigned len, dma_addr_t buf)
  330. {
  331. struct s3c64xx_spi_driver_data *sdd;
  332. struct dma_slave_config config;
  333. struct scatterlist sg;
  334. struct dma_async_tx_descriptor *desc;
  335. if (dma->direction == DMA_DEV_TO_MEM) {
  336. sdd = container_of((void *)dma,
  337. struct s3c64xx_spi_driver_data, rx_dma);
  338. config.direction = dma->direction;
  339. config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
  340. config.src_addr_width = sdd->cur_bpw / 8;
  341. config.src_maxburst = 1;
  342. dmaengine_slave_config(dma->ch, &config);
  343. } else {
  344. sdd = container_of((void *)dma,
  345. struct s3c64xx_spi_driver_data, tx_dma);
  346. config.direction = dma->direction;
  347. config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
  348. config.dst_addr_width = sdd->cur_bpw / 8;
  349. config.dst_maxburst = 1;
  350. dmaengine_slave_config(dma->ch, &config);
  351. }
  352. sg_init_table(&sg, 1);
  353. sg_dma_len(&sg) = len;
  354. sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
  355. len, offset_in_page(buf));
  356. sg_dma_address(&sg) = buf;
  357. desc = dmaengine_prep_slave_sg(dma->ch,
  358. &sg, 1, dma->direction, DMA_PREP_INTERRUPT);
  359. desc->callback = s3c64xx_spi_dmacb;
  360. desc->callback_param = dma;
  361. dmaengine_submit(desc);
  362. dma_async_issue_pending(dma->ch);
  363. }
  364. static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
  365. {
  366. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  367. dma_filter_fn filter = sdd->cntrlr_info->filter;
  368. struct device *dev = &sdd->pdev->dev;
  369. dma_cap_mask_t mask;
  370. int ret;
  371. dma_cap_zero(mask);
  372. dma_cap_set(DMA_SLAVE, mask);
  373. /* Acquire DMA channels */
  374. sdd->rx_dma.ch = dma_request_slave_channel_compat(mask, filter,
  375. (void*)sdd->rx_dma.dmach, dev, "rx");
  376. if (!sdd->rx_dma.ch) {
  377. dev_err(dev, "Failed to get RX DMA channel\n");
  378. ret = -EBUSY;
  379. goto out;
  380. }
  381. sdd->tx_dma.ch = dma_request_slave_channel_compat(mask, filter,
  382. (void*)sdd->tx_dma.dmach, dev, "tx");
  383. if (!sdd->tx_dma.ch) {
  384. dev_err(dev, "Failed to get TX DMA channel\n");
  385. ret = -EBUSY;
  386. goto out_rx;
  387. }
  388. ret = pm_runtime_get_sync(&sdd->pdev->dev);
  389. if (ret != 0) {
  390. dev_err(dev, "Failed to enable device: %d\n", ret);
  391. goto out_tx;
  392. }
  393. return 0;
  394. out_tx:
  395. dma_release_channel(sdd->tx_dma.ch);
  396. out_rx:
  397. dma_release_channel(sdd->rx_dma.ch);
  398. out:
  399. return ret;
  400. }
  401. static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
  402. {
  403. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  404. /* Free DMA channels */
  405. if (!is_polling(sdd)) {
  406. dma_release_channel(sdd->rx_dma.ch);
  407. dma_release_channel(sdd->tx_dma.ch);
  408. }
  409. pm_runtime_put(&sdd->pdev->dev);
  410. return 0;
  411. }
  412. static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
  413. struct s3c64xx_spi_dma_data *dma)
  414. {
  415. dmaengine_terminate_all(dma->ch);
  416. }
  417. #endif
  418. static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
  419. struct spi_device *spi,
  420. struct spi_transfer *xfer, int dma_mode)
  421. {
  422. void __iomem *regs = sdd->regs;
  423. u32 modecfg, chcfg;
  424. modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
  425. modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
  426. chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
  427. chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
  428. if (dma_mode) {
  429. chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
  430. } else {
  431. /* Always shift in data in FIFO, even if xfer is Tx only,
  432. * this helps setting PCKT_CNT value for generating clocks
  433. * as exactly needed.
  434. */
  435. chcfg |= S3C64XX_SPI_CH_RXCH_ON;
  436. writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
  437. | S3C64XX_SPI_PACKET_CNT_EN,
  438. regs + S3C64XX_SPI_PACKET_CNT);
  439. }
  440. if (xfer->tx_buf != NULL) {
  441. sdd->state |= TXBUSY;
  442. chcfg |= S3C64XX_SPI_CH_TXCH_ON;
  443. if (dma_mode) {
  444. modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
  445. prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
  446. } else {
  447. switch (sdd->cur_bpw) {
  448. case 32:
  449. iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
  450. xfer->tx_buf, xfer->len / 4);
  451. break;
  452. case 16:
  453. iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
  454. xfer->tx_buf, xfer->len / 2);
  455. break;
  456. default:
  457. iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
  458. xfer->tx_buf, xfer->len);
  459. break;
  460. }
  461. }
  462. }
  463. if (xfer->rx_buf != NULL) {
  464. sdd->state |= RXBUSY;
  465. if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
  466. && !(sdd->cur_mode & SPI_CPHA))
  467. chcfg |= S3C64XX_SPI_CH_HS_EN;
  468. if (dma_mode) {
  469. modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
  470. chcfg |= S3C64XX_SPI_CH_RXCH_ON;
  471. writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
  472. | S3C64XX_SPI_PACKET_CNT_EN,
  473. regs + S3C64XX_SPI_PACKET_CNT);
  474. prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
  475. }
  476. }
  477. writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
  478. writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
  479. }
  480. static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
  481. struct spi_device *spi)
  482. {
  483. struct s3c64xx_spi_csinfo *cs;
  484. if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
  485. if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
  486. /* Deselect the last toggled device */
  487. cs = sdd->tgl_spi->controller_data;
  488. if (sdd->cs_gpio)
  489. gpio_set_value(cs->line,
  490. spi->mode & SPI_CS_HIGH ? 0 : 1);
  491. }
  492. sdd->tgl_spi = NULL;
  493. }
  494. cs = spi->controller_data;
  495. if (sdd->cs_gpio)
  496. gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
  497. /* Start the signals */
  498. writel(0, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
  499. }
  500. static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,
  501. int timeout_ms)
  502. {
  503. void __iomem *regs = sdd->regs;
  504. unsigned long val = 1;
  505. u32 status;
  506. /* max fifo depth available */
  507. u32 max_fifo = (FIFO_LVL_MASK(sdd) >> 1) + 1;
  508. if (timeout_ms)
  509. val = msecs_to_loops(timeout_ms);
  510. do {
  511. status = readl(regs + S3C64XX_SPI_STATUS);
  512. } while (RX_FIFO_LVL(status, sdd) < max_fifo && --val);
  513. /* return the actual received data length */
  514. return RX_FIFO_LVL(status, sdd);
  515. }
  516. static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
  517. struct spi_transfer *xfer, int dma_mode)
  518. {
  519. void __iomem *regs = sdd->regs;
  520. unsigned long val;
  521. int ms;
  522. /* millisecs to xfer 'len' bytes @ 'cur_speed' */
  523. ms = xfer->len * 8 * 1000 / sdd->cur_speed;
  524. ms += 10; /* some tolerance */
  525. if (dma_mode) {
  526. val = msecs_to_jiffies(ms) + 10;
  527. val = wait_for_completion_timeout(&sdd->xfer_completion, val);
  528. } else {
  529. u32 status;
  530. val = msecs_to_loops(ms);
  531. do {
  532. status = readl(regs + S3C64XX_SPI_STATUS);
  533. } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
  534. }
  535. if (dma_mode) {
  536. u32 status;
  537. /*
  538. * If the previous xfer was completed within timeout, then
  539. * proceed further else return -EIO.
  540. * DmaTx returns after simply writing data in the FIFO,
  541. * w/o waiting for real transmission on the bus to finish.
  542. * DmaRx returns only after Dma read data from FIFO which
  543. * needs bus transmission to finish, so we don't worry if
  544. * Xfer involved Rx(with or without Tx).
  545. */
  546. if (val && !xfer->rx_buf) {
  547. val = msecs_to_loops(10);
  548. status = readl(regs + S3C64XX_SPI_STATUS);
  549. while ((TX_FIFO_LVL(status, sdd)
  550. || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
  551. && --val) {
  552. cpu_relax();
  553. status = readl(regs + S3C64XX_SPI_STATUS);
  554. }
  555. }
  556. /* If timed out while checking rx/tx status return error */
  557. if (!val)
  558. return -EIO;
  559. } else {
  560. int loops;
  561. u32 cpy_len;
  562. u8 *buf;
  563. /* If it was only Tx */
  564. if (!xfer->rx_buf) {
  565. sdd->state &= ~TXBUSY;
  566. return 0;
  567. }
  568. /*
  569. * If the receive length is bigger than the controller fifo
  570. * size, calculate the loops and read the fifo as many times.
  571. * loops = length / max fifo size (calculated by using the
  572. * fifo mask).
  573. * For any size less than the fifo size the below code is
  574. * executed atleast once.
  575. */
  576. loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);
  577. buf = xfer->rx_buf;
  578. do {
  579. /* wait for data to be received in the fifo */
  580. cpy_len = s3c64xx_spi_wait_for_timeout(sdd,
  581. (loops ? ms : 0));
  582. switch (sdd->cur_bpw) {
  583. case 32:
  584. ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
  585. buf, cpy_len / 4);
  586. break;
  587. case 16:
  588. ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
  589. buf, cpy_len / 2);
  590. break;
  591. default:
  592. ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
  593. buf, cpy_len);
  594. break;
  595. }
  596. buf = buf + cpy_len;
  597. } while (loops--);
  598. sdd->state &= ~RXBUSY;
  599. }
  600. return 0;
  601. }
  602. static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
  603. struct spi_device *spi)
  604. {
  605. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  606. if (sdd->tgl_spi == spi)
  607. sdd->tgl_spi = NULL;
  608. if (sdd->cs_gpio)
  609. gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
  610. /* Quiese the signals */
  611. writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
  612. }
  613. static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
  614. {
  615. void __iomem *regs = sdd->regs;
  616. u32 val;
  617. /* Disable Clock */
  618. if (sdd->port_conf->clk_from_cmu) {
  619. clk_disable_unprepare(sdd->src_clk);
  620. } else {
  621. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  622. val &= ~S3C64XX_SPI_ENCLK_ENABLE;
  623. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  624. }
  625. /* Set Polarity and Phase */
  626. val = readl(regs + S3C64XX_SPI_CH_CFG);
  627. val &= ~(S3C64XX_SPI_CH_SLAVE |
  628. S3C64XX_SPI_CPOL_L |
  629. S3C64XX_SPI_CPHA_B);
  630. if (sdd->cur_mode & SPI_CPOL)
  631. val |= S3C64XX_SPI_CPOL_L;
  632. if (sdd->cur_mode & SPI_CPHA)
  633. val |= S3C64XX_SPI_CPHA_B;
  634. writel(val, regs + S3C64XX_SPI_CH_CFG);
  635. /* Set Channel & DMA Mode */
  636. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  637. val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
  638. | S3C64XX_SPI_MODE_CH_TSZ_MASK);
  639. switch (sdd->cur_bpw) {
  640. case 32:
  641. val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
  642. val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
  643. break;
  644. case 16:
  645. val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
  646. val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
  647. break;
  648. default:
  649. val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
  650. val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
  651. break;
  652. }
  653. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  654. if (sdd->port_conf->clk_from_cmu) {
  655. /* Configure Clock */
  656. /* There is half-multiplier before the SPI */
  657. clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
  658. /* Enable Clock */
  659. clk_prepare_enable(sdd->src_clk);
  660. } else {
  661. /* Configure Clock */
  662. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  663. val &= ~S3C64XX_SPI_PSR_MASK;
  664. val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
  665. & S3C64XX_SPI_PSR_MASK);
  666. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  667. /* Enable Clock */
  668. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  669. val |= S3C64XX_SPI_ENCLK_ENABLE;
  670. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  671. }
  672. }
  673. #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
  674. static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
  675. struct spi_message *msg)
  676. {
  677. struct device *dev = &sdd->pdev->dev;
  678. struct spi_transfer *xfer;
  679. if (is_polling(sdd) || msg->is_dma_mapped)
  680. return 0;
  681. /* First mark all xfer unmapped */
  682. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  683. xfer->rx_dma = XFER_DMAADDR_INVALID;
  684. xfer->tx_dma = XFER_DMAADDR_INVALID;
  685. }
  686. /* Map until end or first fail */
  687. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  688. if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
  689. continue;
  690. if (xfer->tx_buf != NULL) {
  691. xfer->tx_dma = dma_map_single(dev,
  692. (void *)xfer->tx_buf, xfer->len,
  693. DMA_TO_DEVICE);
  694. if (dma_mapping_error(dev, xfer->tx_dma)) {
  695. dev_err(dev, "dma_map_single Tx failed\n");
  696. xfer->tx_dma = XFER_DMAADDR_INVALID;
  697. return -ENOMEM;
  698. }
  699. }
  700. if (xfer->rx_buf != NULL) {
  701. xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
  702. xfer->len, DMA_FROM_DEVICE);
  703. if (dma_mapping_error(dev, xfer->rx_dma)) {
  704. dev_err(dev, "dma_map_single Rx failed\n");
  705. dma_unmap_single(dev, xfer->tx_dma,
  706. xfer->len, DMA_TO_DEVICE);
  707. xfer->tx_dma = XFER_DMAADDR_INVALID;
  708. xfer->rx_dma = XFER_DMAADDR_INVALID;
  709. return -ENOMEM;
  710. }
  711. }
  712. }
  713. return 0;
  714. }
  715. static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
  716. struct spi_message *msg)
  717. {
  718. struct device *dev = &sdd->pdev->dev;
  719. struct spi_transfer *xfer;
  720. if (is_polling(sdd) || msg->is_dma_mapped)
  721. return;
  722. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  723. if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
  724. continue;
  725. if (xfer->rx_buf != NULL
  726. && xfer->rx_dma != XFER_DMAADDR_INVALID)
  727. dma_unmap_single(dev, xfer->rx_dma,
  728. xfer->len, DMA_FROM_DEVICE);
  729. if (xfer->tx_buf != NULL
  730. && xfer->tx_dma != XFER_DMAADDR_INVALID)
  731. dma_unmap_single(dev, xfer->tx_dma,
  732. xfer->len, DMA_TO_DEVICE);
  733. }
  734. }
  735. static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
  736. struct spi_message *msg)
  737. {
  738. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  739. struct spi_device *spi = msg->spi;
  740. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  741. struct spi_transfer *xfer;
  742. int status = 0, cs_toggle = 0;
  743. u32 speed;
  744. u8 bpw;
  745. /* If Master's(controller) state differs from that needed by Slave */
  746. if (sdd->cur_speed != spi->max_speed_hz
  747. || sdd->cur_mode != spi->mode
  748. || sdd->cur_bpw != spi->bits_per_word) {
  749. sdd->cur_bpw = spi->bits_per_word;
  750. sdd->cur_speed = spi->max_speed_hz;
  751. sdd->cur_mode = spi->mode;
  752. s3c64xx_spi_config(sdd);
  753. }
  754. /* Map all the transfers if needed */
  755. if (s3c64xx_spi_map_mssg(sdd, msg)) {
  756. dev_err(&spi->dev,
  757. "Xfer: Unable to map message buffers!\n");
  758. status = -ENOMEM;
  759. goto out;
  760. }
  761. /* Configure feedback delay */
  762. writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
  763. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  764. unsigned long flags;
  765. int use_dma;
  766. INIT_COMPLETION(sdd->xfer_completion);
  767. /* Only BPW and Speed may change across transfers */
  768. bpw = xfer->bits_per_word;
  769. speed = xfer->speed_hz ? : spi->max_speed_hz;
  770. if (xfer->len % (bpw / 8)) {
  771. dev_err(&spi->dev,
  772. "Xfer length(%u) not a multiple of word size(%u)\n",
  773. xfer->len, bpw / 8);
  774. status = -EIO;
  775. goto out;
  776. }
  777. if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
  778. sdd->cur_bpw = bpw;
  779. sdd->cur_speed = speed;
  780. s3c64xx_spi_config(sdd);
  781. }
  782. /* Polling method for xfers not bigger than FIFO capacity */
  783. use_dma = 0;
  784. if (!is_polling(sdd) &&
  785. (sdd->rx_dma.ch && sdd->tx_dma.ch &&
  786. (xfer->len > ((FIFO_LVL_MASK(sdd) >> 1) + 1))))
  787. use_dma = 1;
  788. spin_lock_irqsave(&sdd->lock, flags);
  789. /* Pending only which is to be done */
  790. sdd->state &= ~RXBUSY;
  791. sdd->state &= ~TXBUSY;
  792. enable_datapath(sdd, spi, xfer, use_dma);
  793. /* Slave Select */
  794. enable_cs(sdd, spi);
  795. spin_unlock_irqrestore(&sdd->lock, flags);
  796. status = wait_for_xfer(sdd, xfer, use_dma);
  797. if (status) {
  798. dev_err(&spi->dev, "I/O Error: rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
  799. xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
  800. (sdd->state & RXBUSY) ? 'f' : 'p',
  801. (sdd->state & TXBUSY) ? 'f' : 'p',
  802. xfer->len);
  803. if (use_dma) {
  804. if (xfer->tx_buf != NULL
  805. && (sdd->state & TXBUSY))
  806. s3c64xx_spi_dma_stop(sdd, &sdd->tx_dma);
  807. if (xfer->rx_buf != NULL
  808. && (sdd->state & RXBUSY))
  809. s3c64xx_spi_dma_stop(sdd, &sdd->rx_dma);
  810. }
  811. goto out;
  812. }
  813. if (xfer->delay_usecs)
  814. udelay(xfer->delay_usecs);
  815. if (xfer->cs_change) {
  816. /* Hint that the next mssg is gonna be
  817. for the same device */
  818. if (list_is_last(&xfer->transfer_list,
  819. &msg->transfers))
  820. cs_toggle = 1;
  821. }
  822. msg->actual_length += xfer->len;
  823. flush_fifo(sdd);
  824. }
  825. out:
  826. if (!cs_toggle || status)
  827. disable_cs(sdd, spi);
  828. else
  829. sdd->tgl_spi = spi;
  830. s3c64xx_spi_unmap_mssg(sdd, msg);
  831. msg->status = status;
  832. spi_finalize_current_message(master);
  833. return 0;
  834. }
  835. static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
  836. struct spi_device *spi)
  837. {
  838. struct s3c64xx_spi_csinfo *cs;
  839. struct device_node *slave_np, *data_np = NULL;
  840. struct s3c64xx_spi_driver_data *sdd;
  841. u32 fb_delay = 0;
  842. sdd = spi_master_get_devdata(spi->master);
  843. slave_np = spi->dev.of_node;
  844. if (!slave_np) {
  845. dev_err(&spi->dev, "device node not found\n");
  846. return ERR_PTR(-EINVAL);
  847. }
  848. data_np = of_get_child_by_name(slave_np, "controller-data");
  849. if (!data_np) {
  850. dev_err(&spi->dev, "child node 'controller-data' not found\n");
  851. return ERR_PTR(-EINVAL);
  852. }
  853. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  854. if (!cs) {
  855. dev_err(&spi->dev, "could not allocate memory for controller data\n");
  856. of_node_put(data_np);
  857. return ERR_PTR(-ENOMEM);
  858. }
  859. /* The CS line is asserted/deasserted by the gpio pin */
  860. if (sdd->cs_gpio)
  861. cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
  862. if (!gpio_is_valid(cs->line)) {
  863. dev_err(&spi->dev, "chip select gpio is not specified or invalid\n");
  864. kfree(cs);
  865. of_node_put(data_np);
  866. return ERR_PTR(-EINVAL);
  867. }
  868. of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
  869. cs->fb_delay = fb_delay;
  870. of_node_put(data_np);
  871. return cs;
  872. }
  873. /*
  874. * Here we only check the validity of requested configuration
  875. * and save the configuration in a local data-structure.
  876. * The controller is actually configured only just before we
  877. * get a message to transfer.
  878. */
  879. static int s3c64xx_spi_setup(struct spi_device *spi)
  880. {
  881. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  882. struct s3c64xx_spi_driver_data *sdd;
  883. struct s3c64xx_spi_info *sci;
  884. struct spi_message *msg;
  885. unsigned long flags;
  886. int err;
  887. sdd = spi_master_get_devdata(spi->master);
  888. if (!cs && spi->dev.of_node) {
  889. cs = s3c64xx_get_slave_ctrldata(spi);
  890. spi->controller_data = cs;
  891. }
  892. if (IS_ERR_OR_NULL(cs)) {
  893. dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
  894. return -ENODEV;
  895. }
  896. /* Request gpio only if cs line is asserted by gpio pins */
  897. if (sdd->cs_gpio) {
  898. err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
  899. dev_name(&spi->dev));
  900. if (err) {
  901. dev_err(&spi->dev,
  902. "Failed to get /CS gpio [%d]: %d\n",
  903. cs->line, err);
  904. goto err_gpio_req;
  905. }
  906. }
  907. if (!spi_get_ctldata(spi))
  908. spi_set_ctldata(spi, cs);
  909. sci = sdd->cntrlr_info;
  910. spin_lock_irqsave(&sdd->lock, flags);
  911. list_for_each_entry(msg, &sdd->queue, queue) {
  912. /* Is some mssg is already queued for this device */
  913. if (msg->spi == spi) {
  914. dev_err(&spi->dev,
  915. "setup: attempt while mssg in queue!\n");
  916. spin_unlock_irqrestore(&sdd->lock, flags);
  917. err = -EBUSY;
  918. goto err_msgq;
  919. }
  920. }
  921. spin_unlock_irqrestore(&sdd->lock, flags);
  922. pm_runtime_get_sync(&sdd->pdev->dev);
  923. /* Check if we can provide the requested rate */
  924. if (!sdd->port_conf->clk_from_cmu) {
  925. u32 psr, speed;
  926. /* Max possible */
  927. speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
  928. if (spi->max_speed_hz > speed)
  929. spi->max_speed_hz = speed;
  930. psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
  931. psr &= S3C64XX_SPI_PSR_MASK;
  932. if (psr == S3C64XX_SPI_PSR_MASK)
  933. psr--;
  934. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  935. if (spi->max_speed_hz < speed) {
  936. if (psr+1 < S3C64XX_SPI_PSR_MASK) {
  937. psr++;
  938. } else {
  939. err = -EINVAL;
  940. goto setup_exit;
  941. }
  942. }
  943. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  944. if (spi->max_speed_hz >= speed) {
  945. spi->max_speed_hz = speed;
  946. } else {
  947. dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
  948. spi->max_speed_hz);
  949. err = -EINVAL;
  950. goto setup_exit;
  951. }
  952. }
  953. pm_runtime_put(&sdd->pdev->dev);
  954. disable_cs(sdd, spi);
  955. return 0;
  956. setup_exit:
  957. /* setup() returns with device de-selected */
  958. disable_cs(sdd, spi);
  959. err_msgq:
  960. gpio_free(cs->line);
  961. spi_set_ctldata(spi, NULL);
  962. err_gpio_req:
  963. if (spi->dev.of_node)
  964. kfree(cs);
  965. return err;
  966. }
  967. static void s3c64xx_spi_cleanup(struct spi_device *spi)
  968. {
  969. struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
  970. struct s3c64xx_spi_driver_data *sdd;
  971. sdd = spi_master_get_devdata(spi->master);
  972. if (cs && sdd->cs_gpio) {
  973. gpio_free(cs->line);
  974. if (spi->dev.of_node)
  975. kfree(cs);
  976. }
  977. spi_set_ctldata(spi, NULL);
  978. }
  979. static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
  980. {
  981. struct s3c64xx_spi_driver_data *sdd = data;
  982. struct spi_master *spi = sdd->master;
  983. unsigned int val, clr = 0;
  984. val = readl(sdd->regs + S3C64XX_SPI_STATUS);
  985. if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
  986. clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
  987. dev_err(&spi->dev, "RX overrun\n");
  988. }
  989. if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
  990. clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
  991. dev_err(&spi->dev, "RX underrun\n");
  992. }
  993. if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
  994. clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
  995. dev_err(&spi->dev, "TX overrun\n");
  996. }
  997. if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
  998. clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
  999. dev_err(&spi->dev, "TX underrun\n");
  1000. }
  1001. /* Clear the pending irq by setting and then clearing it */
  1002. writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
  1003. writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
  1004. return IRQ_HANDLED;
  1005. }
  1006. static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
  1007. {
  1008. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  1009. void __iomem *regs = sdd->regs;
  1010. unsigned int val;
  1011. sdd->cur_speed = 0;
  1012. writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
  1013. /* Disable Interrupts - we use Polling if not DMA mode */
  1014. writel(0, regs + S3C64XX_SPI_INT_EN);
  1015. if (!sdd->port_conf->clk_from_cmu)
  1016. writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
  1017. regs + S3C64XX_SPI_CLK_CFG);
  1018. writel(0, regs + S3C64XX_SPI_MODE_CFG);
  1019. writel(0, regs + S3C64XX_SPI_PACKET_CNT);
  1020. /* Clear any irq pending bits, should set and clear the bits */
  1021. val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
  1022. S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
  1023. S3C64XX_SPI_PND_TX_OVERRUN_CLR |
  1024. S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
  1025. writel(val, regs + S3C64XX_SPI_PENDING_CLR);
  1026. writel(0, regs + S3C64XX_SPI_PENDING_CLR);
  1027. writel(0, regs + S3C64XX_SPI_SWAP_CFG);
  1028. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  1029. val &= ~S3C64XX_SPI_MODE_4BURST;
  1030. val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  1031. val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  1032. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  1033. flush_fifo(sdd);
  1034. }
  1035. #ifdef CONFIG_OF
  1036. static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
  1037. {
  1038. struct s3c64xx_spi_info *sci;
  1039. u32 temp;
  1040. sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
  1041. if (!sci) {
  1042. dev_err(dev, "memory allocation for spi_info failed\n");
  1043. return ERR_PTR(-ENOMEM);
  1044. }
  1045. if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
  1046. dev_warn(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
  1047. sci->src_clk_nr = 0;
  1048. } else {
  1049. sci->src_clk_nr = temp;
  1050. }
  1051. if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
  1052. dev_warn(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
  1053. sci->num_cs = 1;
  1054. } else {
  1055. sci->num_cs = temp;
  1056. }
  1057. return sci;
  1058. }
  1059. #else
  1060. static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
  1061. {
  1062. return dev->platform_data;
  1063. }
  1064. #endif
  1065. static const struct of_device_id s3c64xx_spi_dt_match[];
  1066. static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
  1067. struct platform_device *pdev)
  1068. {
  1069. #ifdef CONFIG_OF
  1070. if (pdev->dev.of_node) {
  1071. const struct of_device_id *match;
  1072. match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
  1073. return (struct s3c64xx_spi_port_config *)match->data;
  1074. }
  1075. #endif
  1076. return (struct s3c64xx_spi_port_config *)
  1077. platform_get_device_id(pdev)->driver_data;
  1078. }
  1079. static int s3c64xx_spi_probe(struct platform_device *pdev)
  1080. {
  1081. struct resource *mem_res;
  1082. struct resource *res;
  1083. struct s3c64xx_spi_driver_data *sdd;
  1084. struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
  1085. struct spi_master *master;
  1086. int ret, irq;
  1087. char clk_name[16];
  1088. if (!sci && pdev->dev.of_node) {
  1089. sci = s3c64xx_spi_parse_dt(&pdev->dev);
  1090. if (IS_ERR(sci))
  1091. return PTR_ERR(sci);
  1092. }
  1093. if (!sci) {
  1094. dev_err(&pdev->dev, "platform_data missing!\n");
  1095. return -ENODEV;
  1096. }
  1097. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1098. if (mem_res == NULL) {
  1099. dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
  1100. return -ENXIO;
  1101. }
  1102. irq = platform_get_irq(pdev, 0);
  1103. if (irq < 0) {
  1104. dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  1105. return irq;
  1106. }
  1107. master = spi_alloc_master(&pdev->dev,
  1108. sizeof(struct s3c64xx_spi_driver_data));
  1109. if (master == NULL) {
  1110. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  1111. return -ENOMEM;
  1112. }
  1113. platform_set_drvdata(pdev, master);
  1114. sdd = spi_master_get_devdata(master);
  1115. sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
  1116. sdd->master = master;
  1117. sdd->cntrlr_info = sci;
  1118. sdd->pdev = pdev;
  1119. sdd->sfr_start = mem_res->start;
  1120. sdd->cs_gpio = true;
  1121. if (pdev->dev.of_node) {
  1122. if (!of_find_property(pdev->dev.of_node, "cs-gpio", NULL))
  1123. sdd->cs_gpio = false;
  1124. ret = of_alias_get_id(pdev->dev.of_node, "spi");
  1125. if (ret < 0) {
  1126. dev_err(&pdev->dev, "failed to get alias id, errno %d\n",
  1127. ret);
  1128. goto err0;
  1129. }
  1130. sdd->port_id = ret;
  1131. } else {
  1132. sdd->port_id = pdev->id;
  1133. }
  1134. sdd->cur_bpw = 8;
  1135. if (!sdd->pdev->dev.of_node) {
  1136. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1137. if (!res) {
  1138. dev_warn(&pdev->dev, "Unable to get SPI tx dma "
  1139. "resource. Switching to poll mode\n");
  1140. sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
  1141. } else
  1142. sdd->tx_dma.dmach = res->start;
  1143. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1144. if (!res) {
  1145. dev_warn(&pdev->dev, "Unable to get SPI rx dma "
  1146. "resource. Switching to poll mode\n");
  1147. sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
  1148. } else
  1149. sdd->rx_dma.dmach = res->start;
  1150. }
  1151. sdd->tx_dma.direction = DMA_MEM_TO_DEV;
  1152. sdd->rx_dma.direction = DMA_DEV_TO_MEM;
  1153. master->dev.of_node = pdev->dev.of_node;
  1154. master->bus_num = sdd->port_id;
  1155. master->setup = s3c64xx_spi_setup;
  1156. master->cleanup = s3c64xx_spi_cleanup;
  1157. master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
  1158. master->transfer_one_message = s3c64xx_spi_transfer_one_message;
  1159. master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
  1160. master->num_chipselect = sci->num_cs;
  1161. master->dma_alignment = 8;
  1162. master->bits_per_word_mask = BIT(32 - 1) | BIT(16 - 1) | BIT(8 - 1);
  1163. /* the spi->mode bits understood by this driver: */
  1164. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1165. sdd->regs = devm_ioremap_resource(&pdev->dev, mem_res);
  1166. if (IS_ERR(sdd->regs)) {
  1167. ret = PTR_ERR(sdd->regs);
  1168. goto err0;
  1169. }
  1170. if (sci->cfg_gpio && sci->cfg_gpio()) {
  1171. dev_err(&pdev->dev, "Unable to config gpio\n");
  1172. ret = -EBUSY;
  1173. goto err0;
  1174. }
  1175. /* Setup clocks */
  1176. sdd->clk = devm_clk_get(&pdev->dev, "spi");
  1177. if (IS_ERR(sdd->clk)) {
  1178. dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
  1179. ret = PTR_ERR(sdd->clk);
  1180. goto err0;
  1181. }
  1182. if (clk_prepare_enable(sdd->clk)) {
  1183. dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
  1184. ret = -EBUSY;
  1185. goto err0;
  1186. }
  1187. sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
  1188. sdd->src_clk = devm_clk_get(&pdev->dev, clk_name);
  1189. if (IS_ERR(sdd->src_clk)) {
  1190. dev_err(&pdev->dev,
  1191. "Unable to acquire clock '%s'\n", clk_name);
  1192. ret = PTR_ERR(sdd->src_clk);
  1193. goto err2;
  1194. }
  1195. if (clk_prepare_enable(sdd->src_clk)) {
  1196. dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
  1197. ret = -EBUSY;
  1198. goto err2;
  1199. }
  1200. /* Setup Deufult Mode */
  1201. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1202. spin_lock_init(&sdd->lock);
  1203. init_completion(&sdd->xfer_completion);
  1204. INIT_LIST_HEAD(&sdd->queue);
  1205. ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
  1206. "spi-s3c64xx", sdd);
  1207. if (ret != 0) {
  1208. dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
  1209. irq, ret);
  1210. goto err3;
  1211. }
  1212. writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
  1213. S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
  1214. sdd->regs + S3C64XX_SPI_INT_EN);
  1215. if (spi_register_master(master)) {
  1216. dev_err(&pdev->dev, "cannot register SPI master\n");
  1217. ret = -EBUSY;
  1218. goto err3;
  1219. }
  1220. dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d with %d Slaves attached\n",
  1221. sdd->port_id, master->num_chipselect);
  1222. dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
  1223. mem_res->end, mem_res->start,
  1224. sdd->rx_dma.dmach, sdd->tx_dma.dmach);
  1225. pm_runtime_enable(&pdev->dev);
  1226. return 0;
  1227. err3:
  1228. clk_disable_unprepare(sdd->src_clk);
  1229. err2:
  1230. clk_disable_unprepare(sdd->clk);
  1231. err0:
  1232. platform_set_drvdata(pdev, NULL);
  1233. spi_master_put(master);
  1234. return ret;
  1235. }
  1236. static int s3c64xx_spi_remove(struct platform_device *pdev)
  1237. {
  1238. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  1239. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1240. pm_runtime_disable(&pdev->dev);
  1241. spi_unregister_master(master);
  1242. writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
  1243. clk_disable_unprepare(sdd->src_clk);
  1244. clk_disable_unprepare(sdd->clk);
  1245. platform_set_drvdata(pdev, NULL);
  1246. spi_master_put(master);
  1247. return 0;
  1248. }
  1249. #ifdef CONFIG_PM_SLEEP
  1250. static int s3c64xx_spi_suspend(struct device *dev)
  1251. {
  1252. struct spi_master *master = dev_get_drvdata(dev);
  1253. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1254. spi_master_suspend(master);
  1255. /* Disable the clock */
  1256. clk_disable_unprepare(sdd->src_clk);
  1257. clk_disable_unprepare(sdd->clk);
  1258. sdd->cur_speed = 0; /* Output Clock is stopped */
  1259. return 0;
  1260. }
  1261. static int s3c64xx_spi_resume(struct device *dev)
  1262. {
  1263. struct spi_master *master = dev_get_drvdata(dev);
  1264. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1265. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  1266. if (sci->cfg_gpio)
  1267. sci->cfg_gpio();
  1268. /* Enable the clock */
  1269. clk_prepare_enable(sdd->src_clk);
  1270. clk_prepare_enable(sdd->clk);
  1271. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1272. spi_master_resume(master);
  1273. return 0;
  1274. }
  1275. #endif /* CONFIG_PM_SLEEP */
  1276. #ifdef CONFIG_PM_RUNTIME
  1277. static int s3c64xx_spi_runtime_suspend(struct device *dev)
  1278. {
  1279. struct spi_master *master = dev_get_drvdata(dev);
  1280. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1281. clk_disable_unprepare(sdd->clk);
  1282. clk_disable_unprepare(sdd->src_clk);
  1283. return 0;
  1284. }
  1285. static int s3c64xx_spi_runtime_resume(struct device *dev)
  1286. {
  1287. struct spi_master *master = dev_get_drvdata(dev);
  1288. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1289. clk_prepare_enable(sdd->src_clk);
  1290. clk_prepare_enable(sdd->clk);
  1291. return 0;
  1292. }
  1293. #endif /* CONFIG_PM_RUNTIME */
  1294. static const struct dev_pm_ops s3c64xx_spi_pm = {
  1295. SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
  1296. SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
  1297. s3c64xx_spi_runtime_resume, NULL)
  1298. };
  1299. static struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
  1300. .fifo_lvl_mask = { 0x7f },
  1301. .rx_lvl_offset = 13,
  1302. .tx_st_done = 21,
  1303. .high_speed = true,
  1304. };
  1305. static struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
  1306. .fifo_lvl_mask = { 0x7f, 0x7F },
  1307. .rx_lvl_offset = 13,
  1308. .tx_st_done = 21,
  1309. };
  1310. static struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
  1311. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1312. .rx_lvl_offset = 15,
  1313. .tx_st_done = 25,
  1314. };
  1315. static struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
  1316. .fifo_lvl_mask = { 0x7f, 0x7F },
  1317. .rx_lvl_offset = 13,
  1318. .tx_st_done = 21,
  1319. .high_speed = true,
  1320. };
  1321. static struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
  1322. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1323. .rx_lvl_offset = 15,
  1324. .tx_st_done = 25,
  1325. .high_speed = true,
  1326. };
  1327. static struct s3c64xx_spi_port_config exynos4_spi_port_config = {
  1328. .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
  1329. .rx_lvl_offset = 15,
  1330. .tx_st_done = 25,
  1331. .high_speed = true,
  1332. .clk_from_cmu = true,
  1333. };
  1334. static struct s3c64xx_spi_port_config exynos5440_spi_port_config = {
  1335. .fifo_lvl_mask = { 0x1ff },
  1336. .rx_lvl_offset = 15,
  1337. .tx_st_done = 25,
  1338. .high_speed = true,
  1339. .clk_from_cmu = true,
  1340. .quirks = S3C64XX_SPI_QUIRK_POLL,
  1341. };
  1342. static struct platform_device_id s3c64xx_spi_driver_ids[] = {
  1343. {
  1344. .name = "s3c2443-spi",
  1345. .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
  1346. }, {
  1347. .name = "s3c6410-spi",
  1348. .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
  1349. }, {
  1350. .name = "s5p64x0-spi",
  1351. .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
  1352. }, {
  1353. .name = "s5pc100-spi",
  1354. .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
  1355. }, {
  1356. .name = "s5pv210-spi",
  1357. .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
  1358. }, {
  1359. .name = "exynos4210-spi",
  1360. .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
  1361. },
  1362. { },
  1363. };
  1364. #ifdef CONFIG_OF
  1365. static const struct of_device_id s3c64xx_spi_dt_match[] = {
  1366. { .compatible = "samsung,exynos4210-spi",
  1367. .data = (void *)&exynos4_spi_port_config,
  1368. },
  1369. { .compatible = "samsung,exynos5440-spi",
  1370. .data = (void *)&exynos5440_spi_port_config,
  1371. },
  1372. { },
  1373. };
  1374. MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
  1375. #endif /* CONFIG_OF */
  1376. static struct platform_driver s3c64xx_spi_driver = {
  1377. .driver = {
  1378. .name = "s3c64xx-spi",
  1379. .owner = THIS_MODULE,
  1380. .pm = &s3c64xx_spi_pm,
  1381. .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
  1382. },
  1383. .remove = s3c64xx_spi_remove,
  1384. .id_table = s3c64xx_spi_driver_ids,
  1385. };
  1386. MODULE_ALIAS("platform:s3c64xx-spi");
  1387. static int __init s3c64xx_spi_init(void)
  1388. {
  1389. return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
  1390. }
  1391. subsys_initcall(s3c64xx_spi_init);
  1392. static void __exit s3c64xx_spi_exit(void)
  1393. {
  1394. platform_driver_unregister(&s3c64xx_spi_driver);
  1395. }
  1396. module_exit(s3c64xx_spi_exit);
  1397. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  1398. MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
  1399. MODULE_LICENSE("GPL");