spi-s3c64xx.c 38 KB

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