spi-s3c64xx.c 37 KB

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