spi-s3c64xx.c 38 KB

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