spi-s3c64xx.c 38 KB

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