spi-s3c64xx.c 37 KB

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