spi-s3c64xx.c 39 KB

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