spi-s3c64xx.c 38 KB

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