spi-s3c64xx.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/spi/spi.h>
  29. #include <mach/dma.h>
  30. #include <plat/s3c64xx-spi.h>
  31. /* Registers and bit-fields */
  32. #define S3C64XX_SPI_CH_CFG 0x00
  33. #define S3C64XX_SPI_CLK_CFG 0x04
  34. #define S3C64XX_SPI_MODE_CFG 0x08
  35. #define S3C64XX_SPI_SLAVE_SEL 0x0C
  36. #define S3C64XX_SPI_INT_EN 0x10
  37. #define S3C64XX_SPI_STATUS 0x14
  38. #define S3C64XX_SPI_TX_DATA 0x18
  39. #define S3C64XX_SPI_RX_DATA 0x1C
  40. #define S3C64XX_SPI_PACKET_CNT 0x20
  41. #define S3C64XX_SPI_PENDING_CLR 0x24
  42. #define S3C64XX_SPI_SWAP_CFG 0x28
  43. #define S3C64XX_SPI_FB_CLK 0x2C
  44. #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
  45. #define S3C64XX_SPI_CH_SW_RST (1<<5)
  46. #define S3C64XX_SPI_CH_SLAVE (1<<4)
  47. #define S3C64XX_SPI_CPOL_L (1<<3)
  48. #define S3C64XX_SPI_CPHA_B (1<<2)
  49. #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
  50. #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
  51. #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
  52. #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
  53. #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
  54. #define S3C64XX_SPI_PSR_MASK 0xff
  55. #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
  56. #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
  57. #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
  58. #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
  59. #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
  60. #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
  61. #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
  62. #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
  63. #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
  64. #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
  65. #define S3C64XX_SPI_MODE_4BURST (1<<0)
  66. #define S3C64XX_SPI_SLAVE_AUTO (1<<1)
  67. #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
  68. #define S3C64XX_SPI_ACT(c) writel(0, (c)->regs + S3C64XX_SPI_SLAVE_SEL)
  69. #define S3C64XX_SPI_DEACT(c) writel(S3C64XX_SPI_SLAVE_SIG_INACT, \
  70. (c)->regs + S3C64XX_SPI_SLAVE_SEL)
  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 S3C64XX_SPI_ST_TRLCNTZ(v, i) ((((v) >> (i)->rx_lvl_offset) & \
  100. (((i)->fifo_lvl_mask + 1))) \
  101. ? 1 : 0)
  102. #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & (1 << (i)->tx_st_done)) ? 1 : 0)
  103. #define TX_FIFO_LVL(v, i) (((v) >> 6) & (i)->fifo_lvl_mask)
  104. #define RX_FIFO_LVL(v, i) (((v) >> (i)->rx_lvl_offset) & (i)->fifo_lvl_mask)
  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 RXBUSY (1<<2)
  110. #define TXBUSY (1<<3)
  111. struct s3c64xx_spi_dma_data {
  112. unsigned ch;
  113. enum dma_data_direction direction;
  114. enum dma_ch dmach;
  115. };
  116. /**
  117. * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
  118. * @clk: Pointer to the spi clock.
  119. * @src_clk: Pointer to the clock used to generate SPI signals.
  120. * @master: Pointer to the SPI Protocol master.
  121. * @cntrlr_info: Platform specific data for the controller this driver manages.
  122. * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
  123. * @queue: To log SPI xfer requests.
  124. * @lock: Controller specific lock.
  125. * @state: Set of FLAGS to indicate status.
  126. * @rx_dmach: Controller's DMA channel for Rx.
  127. * @tx_dmach: Controller's DMA channel for Tx.
  128. * @sfr_start: BUS address of SPI controller regs.
  129. * @regs: Pointer to ioremap'ed controller registers.
  130. * @irq: interrupt
  131. * @xfer_completion: To indicate completion of xfer task.
  132. * @cur_mode: Stores the active configuration of the controller.
  133. * @cur_bpw: Stores the active bits per word settings.
  134. * @cur_speed: Stores the active xfer clock speed.
  135. */
  136. struct s3c64xx_spi_driver_data {
  137. void __iomem *regs;
  138. struct clk *clk;
  139. struct clk *src_clk;
  140. struct platform_device *pdev;
  141. struct spi_master *master;
  142. struct s3c64xx_spi_info *cntrlr_info;
  143. struct spi_device *tgl_spi;
  144. struct list_head queue;
  145. spinlock_t lock;
  146. unsigned long sfr_start;
  147. struct completion xfer_completion;
  148. unsigned state;
  149. unsigned cur_mode, cur_bpw;
  150. unsigned cur_speed;
  151. struct s3c64xx_spi_dma_data rx_dma;
  152. struct s3c64xx_spi_dma_data tx_dma;
  153. struct samsung_dma_ops *ops;
  154. };
  155. static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
  156. .name = "samsung-spi-dma",
  157. };
  158. static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
  159. {
  160. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  161. void __iomem *regs = sdd->regs;
  162. unsigned long loops;
  163. u32 val;
  164. writel(0, regs + S3C64XX_SPI_PACKET_CNT);
  165. val = readl(regs + S3C64XX_SPI_CH_CFG);
  166. val |= S3C64XX_SPI_CH_SW_RST;
  167. val &= ~S3C64XX_SPI_CH_HS_EN;
  168. writel(val, regs + S3C64XX_SPI_CH_CFG);
  169. /* Flush TxFIFO*/
  170. loops = msecs_to_loops(1);
  171. do {
  172. val = readl(regs + S3C64XX_SPI_STATUS);
  173. } while (TX_FIFO_LVL(val, sci) && loops--);
  174. if (loops == 0)
  175. dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
  176. /* Flush RxFIFO*/
  177. loops = msecs_to_loops(1);
  178. do {
  179. val = readl(regs + S3C64XX_SPI_STATUS);
  180. if (RX_FIFO_LVL(val, sci))
  181. readl(regs + S3C64XX_SPI_RX_DATA);
  182. else
  183. break;
  184. } while (loops--);
  185. if (loops == 0)
  186. dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
  187. val = readl(regs + S3C64XX_SPI_CH_CFG);
  188. val &= ~S3C64XX_SPI_CH_SW_RST;
  189. writel(val, regs + S3C64XX_SPI_CH_CFG);
  190. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  191. val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
  192. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  193. val = readl(regs + S3C64XX_SPI_CH_CFG);
  194. val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
  195. writel(val, regs + S3C64XX_SPI_CH_CFG);
  196. }
  197. static void s3c64xx_spi_dmacb(void *data)
  198. {
  199. struct s3c64xx_spi_driver_data *sdd;
  200. struct s3c64xx_spi_dma_data *dma = data;
  201. unsigned long flags;
  202. if (dma->direction == DMA_DEV_TO_MEM)
  203. sdd = container_of(data,
  204. struct s3c64xx_spi_driver_data, rx_dma);
  205. else
  206. sdd = container_of(data,
  207. struct s3c64xx_spi_driver_data, tx_dma);
  208. spin_lock_irqsave(&sdd->lock, flags);
  209. if (dma->direction == DMA_DEV_TO_MEM) {
  210. sdd->state &= ~RXBUSY;
  211. if (!(sdd->state & TXBUSY))
  212. complete(&sdd->xfer_completion);
  213. } else {
  214. sdd->state &= ~TXBUSY;
  215. if (!(sdd->state & RXBUSY))
  216. complete(&sdd->xfer_completion);
  217. }
  218. spin_unlock_irqrestore(&sdd->lock, flags);
  219. }
  220. static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
  221. unsigned len, dma_addr_t buf)
  222. {
  223. struct s3c64xx_spi_driver_data *sdd;
  224. struct samsung_dma_prep_info info;
  225. if (dma->direction == DMA_DEV_TO_MEM)
  226. sdd = container_of((void *)dma,
  227. struct s3c64xx_spi_driver_data, rx_dma);
  228. else
  229. sdd = container_of((void *)dma,
  230. struct s3c64xx_spi_driver_data, tx_dma);
  231. info.cap = DMA_SLAVE;
  232. info.len = len;
  233. info.fp = s3c64xx_spi_dmacb;
  234. info.fp_param = dma;
  235. info.direction = dma->direction;
  236. info.buf = buf;
  237. sdd->ops->prepare(dma->ch, &info);
  238. sdd->ops->trigger(dma->ch);
  239. }
  240. static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
  241. {
  242. struct samsung_dma_info info;
  243. sdd->ops = samsung_dma_get_ops();
  244. info.cap = DMA_SLAVE;
  245. info.client = &s3c64xx_spi_dma_client;
  246. info.width = sdd->cur_bpw / 8;
  247. info.direction = sdd->rx_dma.direction;
  248. info.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
  249. sdd->rx_dma.ch = sdd->ops->request(sdd->rx_dma.dmach, &info);
  250. info.direction = sdd->tx_dma.direction;
  251. info.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
  252. sdd->tx_dma.ch = sdd->ops->request(sdd->tx_dma.dmach, &info);
  253. return 1;
  254. }
  255. static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
  256. struct spi_device *spi,
  257. struct spi_transfer *xfer, int dma_mode)
  258. {
  259. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  260. void __iomem *regs = sdd->regs;
  261. u32 modecfg, chcfg;
  262. modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
  263. modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
  264. chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
  265. chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
  266. if (dma_mode) {
  267. chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
  268. } else {
  269. /* Always shift in data in FIFO, even if xfer is Tx only,
  270. * this helps setting PCKT_CNT value for generating clocks
  271. * as exactly needed.
  272. */
  273. chcfg |= S3C64XX_SPI_CH_RXCH_ON;
  274. writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
  275. | S3C64XX_SPI_PACKET_CNT_EN,
  276. regs + S3C64XX_SPI_PACKET_CNT);
  277. }
  278. if (xfer->tx_buf != NULL) {
  279. sdd->state |= TXBUSY;
  280. chcfg |= S3C64XX_SPI_CH_TXCH_ON;
  281. if (dma_mode) {
  282. modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
  283. prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
  284. } else {
  285. switch (sdd->cur_bpw) {
  286. case 32:
  287. iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
  288. xfer->tx_buf, xfer->len / 4);
  289. break;
  290. case 16:
  291. iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
  292. xfer->tx_buf, xfer->len / 2);
  293. break;
  294. default:
  295. iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
  296. xfer->tx_buf, xfer->len);
  297. break;
  298. }
  299. }
  300. }
  301. if (xfer->rx_buf != NULL) {
  302. sdd->state |= RXBUSY;
  303. if (sci->high_speed && sdd->cur_speed >= 30000000UL
  304. && !(sdd->cur_mode & SPI_CPHA))
  305. chcfg |= S3C64XX_SPI_CH_HS_EN;
  306. if (dma_mode) {
  307. modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
  308. chcfg |= S3C64XX_SPI_CH_RXCH_ON;
  309. writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
  310. | S3C64XX_SPI_PACKET_CNT_EN,
  311. regs + S3C64XX_SPI_PACKET_CNT);
  312. prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
  313. }
  314. }
  315. writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
  316. writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
  317. }
  318. static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
  319. struct spi_device *spi)
  320. {
  321. struct s3c64xx_spi_csinfo *cs;
  322. if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
  323. if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
  324. /* Deselect the last toggled device */
  325. cs = sdd->tgl_spi->controller_data;
  326. cs->set_level(cs->line,
  327. spi->mode & SPI_CS_HIGH ? 0 : 1);
  328. }
  329. sdd->tgl_spi = NULL;
  330. }
  331. cs = spi->controller_data;
  332. cs->set_level(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
  333. }
  334. static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
  335. struct spi_transfer *xfer, int dma_mode)
  336. {
  337. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  338. void __iomem *regs = sdd->regs;
  339. unsigned long val;
  340. int ms;
  341. /* millisecs to xfer 'len' bytes @ 'cur_speed' */
  342. ms = xfer->len * 8 * 1000 / sdd->cur_speed;
  343. ms += 10; /* some tolerance */
  344. if (dma_mode) {
  345. val = msecs_to_jiffies(ms) + 10;
  346. val = wait_for_completion_timeout(&sdd->xfer_completion, val);
  347. } else {
  348. u32 status;
  349. val = msecs_to_loops(ms);
  350. do {
  351. status = readl(regs + S3C64XX_SPI_STATUS);
  352. } while (RX_FIFO_LVL(status, sci) < xfer->len && --val);
  353. }
  354. if (!val)
  355. return -EIO;
  356. if (dma_mode) {
  357. u32 status;
  358. /*
  359. * DmaTx returns after simply writing data in the FIFO,
  360. * w/o waiting for real transmission on the bus to finish.
  361. * DmaRx returns only after Dma read data from FIFO which
  362. * needs bus transmission to finish, so we don't worry if
  363. * Xfer involved Rx(with or without Tx).
  364. */
  365. if (xfer->rx_buf == NULL) {
  366. val = msecs_to_loops(10);
  367. status = readl(regs + S3C64XX_SPI_STATUS);
  368. while ((TX_FIFO_LVL(status, sci)
  369. || !S3C64XX_SPI_ST_TX_DONE(status, sci))
  370. && --val) {
  371. cpu_relax();
  372. status = readl(regs + S3C64XX_SPI_STATUS);
  373. }
  374. if (!val)
  375. return -EIO;
  376. }
  377. } else {
  378. /* If it was only Tx */
  379. if (xfer->rx_buf == NULL) {
  380. sdd->state &= ~TXBUSY;
  381. return 0;
  382. }
  383. switch (sdd->cur_bpw) {
  384. case 32:
  385. ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
  386. xfer->rx_buf, xfer->len / 4);
  387. break;
  388. case 16:
  389. ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
  390. xfer->rx_buf, xfer->len / 2);
  391. break;
  392. default:
  393. ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
  394. xfer->rx_buf, xfer->len);
  395. break;
  396. }
  397. sdd->state &= ~RXBUSY;
  398. }
  399. return 0;
  400. }
  401. static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
  402. struct spi_device *spi)
  403. {
  404. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  405. if (sdd->tgl_spi == spi)
  406. sdd->tgl_spi = NULL;
  407. cs->set_level(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
  408. }
  409. static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
  410. {
  411. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  412. void __iomem *regs = sdd->regs;
  413. u32 val;
  414. /* Disable Clock */
  415. if (sci->clk_from_cmu) {
  416. clk_disable(sdd->src_clk);
  417. } else {
  418. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  419. val &= ~S3C64XX_SPI_ENCLK_ENABLE;
  420. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  421. }
  422. /* Set Polarity and Phase */
  423. val = readl(regs + S3C64XX_SPI_CH_CFG);
  424. val &= ~(S3C64XX_SPI_CH_SLAVE |
  425. S3C64XX_SPI_CPOL_L |
  426. S3C64XX_SPI_CPHA_B);
  427. if (sdd->cur_mode & SPI_CPOL)
  428. val |= S3C64XX_SPI_CPOL_L;
  429. if (sdd->cur_mode & SPI_CPHA)
  430. val |= S3C64XX_SPI_CPHA_B;
  431. writel(val, regs + S3C64XX_SPI_CH_CFG);
  432. /* Set Channel & DMA Mode */
  433. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  434. val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
  435. | S3C64XX_SPI_MODE_CH_TSZ_MASK);
  436. switch (sdd->cur_bpw) {
  437. case 32:
  438. val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
  439. val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
  440. break;
  441. case 16:
  442. val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
  443. val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
  444. break;
  445. default:
  446. val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
  447. val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
  448. break;
  449. }
  450. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  451. if (sci->clk_from_cmu) {
  452. /* Configure Clock */
  453. /* There is half-multiplier before the SPI */
  454. clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
  455. /* Enable Clock */
  456. clk_enable(sdd->src_clk);
  457. } else {
  458. /* Configure Clock */
  459. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  460. val &= ~S3C64XX_SPI_PSR_MASK;
  461. val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
  462. & S3C64XX_SPI_PSR_MASK);
  463. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  464. /* Enable Clock */
  465. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  466. val |= S3C64XX_SPI_ENCLK_ENABLE;
  467. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  468. }
  469. }
  470. #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
  471. static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
  472. struct spi_message *msg)
  473. {
  474. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  475. struct device *dev = &sdd->pdev->dev;
  476. struct spi_transfer *xfer;
  477. if (msg->is_dma_mapped)
  478. return 0;
  479. /* First mark all xfer unmapped */
  480. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  481. xfer->rx_dma = XFER_DMAADDR_INVALID;
  482. xfer->tx_dma = XFER_DMAADDR_INVALID;
  483. }
  484. /* Map until end or first fail */
  485. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  486. if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1))
  487. continue;
  488. if (xfer->tx_buf != NULL) {
  489. xfer->tx_dma = dma_map_single(dev,
  490. (void *)xfer->tx_buf, xfer->len,
  491. DMA_TO_DEVICE);
  492. if (dma_mapping_error(dev, xfer->tx_dma)) {
  493. dev_err(dev, "dma_map_single Tx failed\n");
  494. xfer->tx_dma = XFER_DMAADDR_INVALID;
  495. return -ENOMEM;
  496. }
  497. }
  498. if (xfer->rx_buf != NULL) {
  499. xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
  500. xfer->len, DMA_FROM_DEVICE);
  501. if (dma_mapping_error(dev, xfer->rx_dma)) {
  502. dev_err(dev, "dma_map_single Rx failed\n");
  503. dma_unmap_single(dev, xfer->tx_dma,
  504. xfer->len, DMA_TO_DEVICE);
  505. xfer->tx_dma = XFER_DMAADDR_INVALID;
  506. xfer->rx_dma = XFER_DMAADDR_INVALID;
  507. return -ENOMEM;
  508. }
  509. }
  510. }
  511. return 0;
  512. }
  513. static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
  514. struct spi_message *msg)
  515. {
  516. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  517. struct device *dev = &sdd->pdev->dev;
  518. struct spi_transfer *xfer;
  519. if (msg->is_dma_mapped)
  520. return;
  521. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  522. if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1))
  523. continue;
  524. if (xfer->rx_buf != NULL
  525. && xfer->rx_dma != XFER_DMAADDR_INVALID)
  526. dma_unmap_single(dev, xfer->rx_dma,
  527. xfer->len, DMA_FROM_DEVICE);
  528. if (xfer->tx_buf != NULL
  529. && xfer->tx_dma != XFER_DMAADDR_INVALID)
  530. dma_unmap_single(dev, xfer->tx_dma,
  531. xfer->len, DMA_TO_DEVICE);
  532. }
  533. }
  534. static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
  535. struct spi_message *msg)
  536. {
  537. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  538. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  539. struct spi_device *spi = msg->spi;
  540. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  541. struct spi_transfer *xfer;
  542. int status = 0, cs_toggle = 0;
  543. u32 speed;
  544. u8 bpw;
  545. /* If Master's(controller) state differs from that needed by Slave */
  546. if (sdd->cur_speed != spi->max_speed_hz
  547. || sdd->cur_mode != spi->mode
  548. || sdd->cur_bpw != spi->bits_per_word) {
  549. sdd->cur_bpw = spi->bits_per_word;
  550. sdd->cur_speed = spi->max_speed_hz;
  551. sdd->cur_mode = spi->mode;
  552. s3c64xx_spi_config(sdd);
  553. }
  554. /* Map all the transfers if needed */
  555. if (s3c64xx_spi_map_mssg(sdd, msg)) {
  556. dev_err(&spi->dev,
  557. "Xfer: Unable to map message buffers!\n");
  558. status = -ENOMEM;
  559. goto out;
  560. }
  561. /* Configure feedback delay */
  562. writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
  563. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  564. unsigned long flags;
  565. int use_dma;
  566. INIT_COMPLETION(sdd->xfer_completion);
  567. /* Only BPW and Speed may change across transfers */
  568. bpw = xfer->bits_per_word ? : spi->bits_per_word;
  569. speed = xfer->speed_hz ? : spi->max_speed_hz;
  570. if (xfer->len % (bpw / 8)) {
  571. dev_err(&spi->dev,
  572. "Xfer length(%u) not a multiple of word size(%u)\n",
  573. xfer->len, bpw / 8);
  574. status = -EIO;
  575. goto out;
  576. }
  577. if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
  578. sdd->cur_bpw = bpw;
  579. sdd->cur_speed = speed;
  580. s3c64xx_spi_config(sdd);
  581. }
  582. /* Polling method for xfers not bigger than FIFO capacity */
  583. if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1))
  584. use_dma = 0;
  585. else
  586. use_dma = 1;
  587. spin_lock_irqsave(&sdd->lock, flags);
  588. /* Pending only which is to be done */
  589. sdd->state &= ~RXBUSY;
  590. sdd->state &= ~TXBUSY;
  591. enable_datapath(sdd, spi, xfer, use_dma);
  592. /* Slave Select */
  593. enable_cs(sdd, spi);
  594. /* Start the signals */
  595. S3C64XX_SPI_ACT(sdd);
  596. spin_unlock_irqrestore(&sdd->lock, flags);
  597. status = wait_for_xfer(sdd, xfer, use_dma);
  598. /* Quiese the signals */
  599. S3C64XX_SPI_DEACT(sdd);
  600. if (status) {
  601. dev_err(&spi->dev, "I/O Error: "
  602. "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
  603. xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
  604. (sdd->state & RXBUSY) ? 'f' : 'p',
  605. (sdd->state & TXBUSY) ? 'f' : 'p',
  606. xfer->len);
  607. if (use_dma) {
  608. if (xfer->tx_buf != NULL
  609. && (sdd->state & TXBUSY))
  610. sdd->ops->stop(sdd->tx_dma.ch);
  611. if (xfer->rx_buf != NULL
  612. && (sdd->state & RXBUSY))
  613. sdd->ops->stop(sdd->rx_dma.ch);
  614. }
  615. goto out;
  616. }
  617. if (xfer->delay_usecs)
  618. udelay(xfer->delay_usecs);
  619. if (xfer->cs_change) {
  620. /* Hint that the next mssg is gonna be
  621. for the same device */
  622. if (list_is_last(&xfer->transfer_list,
  623. &msg->transfers))
  624. cs_toggle = 1;
  625. else
  626. disable_cs(sdd, spi);
  627. }
  628. msg->actual_length += xfer->len;
  629. flush_fifo(sdd);
  630. }
  631. out:
  632. if (!cs_toggle || status)
  633. disable_cs(sdd, spi);
  634. else
  635. sdd->tgl_spi = spi;
  636. s3c64xx_spi_unmap_mssg(sdd, msg);
  637. msg->status = status;
  638. spi_finalize_current_message(master);
  639. return 0;
  640. }
  641. static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
  642. {
  643. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  644. /* Acquire DMA channels */
  645. while (!acquire_dma(sdd))
  646. msleep(10);
  647. pm_runtime_get_sync(&sdd->pdev->dev);
  648. return 0;
  649. }
  650. static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
  651. {
  652. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  653. /* Free DMA channels */
  654. sdd->ops->release(sdd->rx_dma.ch, &s3c64xx_spi_dma_client);
  655. sdd->ops->release(sdd->tx_dma.ch, &s3c64xx_spi_dma_client);
  656. pm_runtime_put(&sdd->pdev->dev);
  657. return 0;
  658. }
  659. /*
  660. * Here we only check the validity of requested configuration
  661. * and save the configuration in a local data-structure.
  662. * The controller is actually configured only just before we
  663. * get a message to transfer.
  664. */
  665. static int s3c64xx_spi_setup(struct spi_device *spi)
  666. {
  667. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  668. struct s3c64xx_spi_driver_data *sdd;
  669. struct s3c64xx_spi_info *sci;
  670. struct spi_message *msg;
  671. unsigned long flags;
  672. int err = 0;
  673. if (cs == NULL || cs->set_level == NULL) {
  674. dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
  675. return -ENODEV;
  676. }
  677. sdd = spi_master_get_devdata(spi->master);
  678. sci = sdd->cntrlr_info;
  679. spin_lock_irqsave(&sdd->lock, flags);
  680. list_for_each_entry(msg, &sdd->queue, queue) {
  681. /* Is some mssg is already queued for this device */
  682. if (msg->spi == spi) {
  683. dev_err(&spi->dev,
  684. "setup: attempt while mssg in queue!\n");
  685. spin_unlock_irqrestore(&sdd->lock, flags);
  686. return -EBUSY;
  687. }
  688. }
  689. spin_unlock_irqrestore(&sdd->lock, flags);
  690. if (spi->bits_per_word != 8
  691. && spi->bits_per_word != 16
  692. && spi->bits_per_word != 32) {
  693. dev_err(&spi->dev, "setup: %dbits/wrd not supported!\n",
  694. spi->bits_per_word);
  695. err = -EINVAL;
  696. goto setup_exit;
  697. }
  698. pm_runtime_get_sync(&sdd->pdev->dev);
  699. /* Check if we can provide the requested rate */
  700. if (!sci->clk_from_cmu) {
  701. u32 psr, speed;
  702. /* Max possible */
  703. speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
  704. if (spi->max_speed_hz > speed)
  705. spi->max_speed_hz = speed;
  706. psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
  707. psr &= S3C64XX_SPI_PSR_MASK;
  708. if (psr == S3C64XX_SPI_PSR_MASK)
  709. psr--;
  710. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  711. if (spi->max_speed_hz < speed) {
  712. if (psr+1 < S3C64XX_SPI_PSR_MASK) {
  713. psr++;
  714. } else {
  715. err = -EINVAL;
  716. goto setup_exit;
  717. }
  718. }
  719. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  720. if (spi->max_speed_hz >= speed)
  721. spi->max_speed_hz = speed;
  722. else
  723. err = -EINVAL;
  724. }
  725. pm_runtime_put(&sdd->pdev->dev);
  726. setup_exit:
  727. /* setup() returns with device de-selected */
  728. disable_cs(sdd, spi);
  729. return err;
  730. }
  731. static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
  732. {
  733. struct s3c64xx_spi_driver_data *sdd = data;
  734. struct spi_master *spi = sdd->master;
  735. unsigned int val, clr = 0;
  736. val = readl(sdd->regs + S3C64XX_SPI_STATUS);
  737. if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
  738. clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
  739. dev_err(&spi->dev, "RX overrun\n");
  740. }
  741. if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
  742. clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
  743. dev_err(&spi->dev, "RX underrun\n");
  744. }
  745. if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
  746. clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
  747. dev_err(&spi->dev, "TX overrun\n");
  748. }
  749. if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
  750. clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
  751. dev_err(&spi->dev, "TX underrun\n");
  752. }
  753. /* Clear the pending irq by setting and then clearing it */
  754. writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
  755. writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
  756. return IRQ_HANDLED;
  757. }
  758. static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
  759. {
  760. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  761. void __iomem *regs = sdd->regs;
  762. unsigned int val;
  763. sdd->cur_speed = 0;
  764. S3C64XX_SPI_DEACT(sdd);
  765. /* Disable Interrupts - we use Polling if not DMA mode */
  766. writel(0, regs + S3C64XX_SPI_INT_EN);
  767. if (!sci->clk_from_cmu)
  768. writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
  769. regs + S3C64XX_SPI_CLK_CFG);
  770. writel(0, regs + S3C64XX_SPI_MODE_CFG);
  771. writel(0, regs + S3C64XX_SPI_PACKET_CNT);
  772. /* Clear any irq pending bits, should set and clear the bits */
  773. val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
  774. S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
  775. S3C64XX_SPI_PND_TX_OVERRUN_CLR |
  776. S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
  777. writel(val, regs + S3C64XX_SPI_PENDING_CLR);
  778. writel(0, regs + S3C64XX_SPI_PENDING_CLR);
  779. writel(0, regs + S3C64XX_SPI_SWAP_CFG);
  780. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  781. val &= ~S3C64XX_SPI_MODE_4BURST;
  782. val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  783. val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  784. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  785. flush_fifo(sdd);
  786. }
  787. static int __init s3c64xx_spi_probe(struct platform_device *pdev)
  788. {
  789. struct resource *mem_res, *dmatx_res, *dmarx_res;
  790. struct s3c64xx_spi_driver_data *sdd;
  791. struct s3c64xx_spi_info *sci;
  792. struct spi_master *master;
  793. int ret, irq;
  794. char clk_name[16];
  795. if (pdev->id < 0) {
  796. dev_err(&pdev->dev,
  797. "Invalid platform device id-%d\n", pdev->id);
  798. return -ENODEV;
  799. }
  800. if (pdev->dev.platform_data == NULL) {
  801. dev_err(&pdev->dev, "platform_data missing!\n");
  802. return -ENODEV;
  803. }
  804. sci = pdev->dev.platform_data;
  805. /* Check for availability of necessary resource */
  806. dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  807. if (dmatx_res == NULL) {
  808. dev_err(&pdev->dev, "Unable to get SPI-Tx dma resource\n");
  809. return -ENXIO;
  810. }
  811. dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  812. if (dmarx_res == NULL) {
  813. dev_err(&pdev->dev, "Unable to get SPI-Rx dma resource\n");
  814. return -ENXIO;
  815. }
  816. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  817. if (mem_res == NULL) {
  818. dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
  819. return -ENXIO;
  820. }
  821. irq = platform_get_irq(pdev, 0);
  822. if (irq < 0) {
  823. dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  824. return irq;
  825. }
  826. master = spi_alloc_master(&pdev->dev,
  827. sizeof(struct s3c64xx_spi_driver_data));
  828. if (master == NULL) {
  829. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  830. return -ENOMEM;
  831. }
  832. platform_set_drvdata(pdev, master);
  833. sdd = spi_master_get_devdata(master);
  834. sdd->master = master;
  835. sdd->cntrlr_info = sci;
  836. sdd->pdev = pdev;
  837. sdd->sfr_start = mem_res->start;
  838. sdd->tx_dma.dmach = dmatx_res->start;
  839. sdd->tx_dma.direction = DMA_MEM_TO_DEV;
  840. sdd->rx_dma.dmach = dmarx_res->start;
  841. sdd->rx_dma.direction = DMA_DEV_TO_MEM;
  842. sdd->cur_bpw = 8;
  843. master->bus_num = pdev->id;
  844. master->setup = s3c64xx_spi_setup;
  845. master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
  846. master->transfer_one_message = s3c64xx_spi_transfer_one_message;
  847. master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
  848. master->num_chipselect = sci->num_cs;
  849. master->dma_alignment = 8;
  850. /* the spi->mode bits understood by this driver: */
  851. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  852. if (request_mem_region(mem_res->start,
  853. resource_size(mem_res), pdev->name) == NULL) {
  854. dev_err(&pdev->dev, "Req mem region failed\n");
  855. ret = -ENXIO;
  856. goto err0;
  857. }
  858. sdd->regs = ioremap(mem_res->start, resource_size(mem_res));
  859. if (sdd->regs == NULL) {
  860. dev_err(&pdev->dev, "Unable to remap IO\n");
  861. ret = -ENXIO;
  862. goto err1;
  863. }
  864. if (sci->cfg_gpio == NULL || sci->cfg_gpio(pdev)) {
  865. dev_err(&pdev->dev, "Unable to config gpio\n");
  866. ret = -EBUSY;
  867. goto err2;
  868. }
  869. /* Setup clocks */
  870. sdd->clk = clk_get(&pdev->dev, "spi");
  871. if (IS_ERR(sdd->clk)) {
  872. dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
  873. ret = PTR_ERR(sdd->clk);
  874. goto err3;
  875. }
  876. if (clk_enable(sdd->clk)) {
  877. dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
  878. ret = -EBUSY;
  879. goto err4;
  880. }
  881. sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
  882. sdd->src_clk = clk_get(&pdev->dev, clk_name);
  883. if (IS_ERR(sdd->src_clk)) {
  884. dev_err(&pdev->dev,
  885. "Unable to acquire clock '%s'\n", clk_name);
  886. ret = PTR_ERR(sdd->src_clk);
  887. goto err5;
  888. }
  889. if (clk_enable(sdd->src_clk)) {
  890. dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
  891. ret = -EBUSY;
  892. goto err6;
  893. }
  894. /* Setup Deufult Mode */
  895. s3c64xx_spi_hwinit(sdd, pdev->id);
  896. spin_lock_init(&sdd->lock);
  897. init_completion(&sdd->xfer_completion);
  898. INIT_LIST_HEAD(&sdd->queue);
  899. ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd);
  900. if (ret != 0) {
  901. dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
  902. irq, ret);
  903. goto err7;
  904. }
  905. writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
  906. S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
  907. sdd->regs + S3C64XX_SPI_INT_EN);
  908. if (spi_register_master(master)) {
  909. dev_err(&pdev->dev, "cannot register SPI master\n");
  910. ret = -EBUSY;
  911. goto err8;
  912. }
  913. dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
  914. "with %d Slaves attached\n",
  915. pdev->id, master->num_chipselect);
  916. dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
  917. mem_res->end, mem_res->start,
  918. sdd->rx_dma.dmach, sdd->tx_dma.dmach);
  919. pm_runtime_enable(&pdev->dev);
  920. return 0;
  921. err8:
  922. free_irq(irq, sdd);
  923. err7:
  924. clk_disable(sdd->src_clk);
  925. err6:
  926. clk_put(sdd->src_clk);
  927. err5:
  928. clk_disable(sdd->clk);
  929. err4:
  930. clk_put(sdd->clk);
  931. err3:
  932. err2:
  933. iounmap((void *) sdd->regs);
  934. err1:
  935. release_mem_region(mem_res->start, resource_size(mem_res));
  936. err0:
  937. platform_set_drvdata(pdev, NULL);
  938. spi_master_put(master);
  939. return ret;
  940. }
  941. static int s3c64xx_spi_remove(struct platform_device *pdev)
  942. {
  943. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  944. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  945. struct resource *mem_res;
  946. pm_runtime_disable(&pdev->dev);
  947. spi_unregister_master(master);
  948. writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
  949. free_irq(platform_get_irq(pdev, 0), sdd);
  950. clk_disable(sdd->src_clk);
  951. clk_put(sdd->src_clk);
  952. clk_disable(sdd->clk);
  953. clk_put(sdd->clk);
  954. iounmap((void *) sdd->regs);
  955. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  956. if (mem_res != NULL)
  957. release_mem_region(mem_res->start, resource_size(mem_res));
  958. platform_set_drvdata(pdev, NULL);
  959. spi_master_put(master);
  960. return 0;
  961. }
  962. #ifdef CONFIG_PM
  963. static int s3c64xx_spi_suspend(struct device *dev)
  964. {
  965. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  966. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  967. spi_master_suspend(master);
  968. /* Disable the clock */
  969. clk_disable(sdd->src_clk);
  970. clk_disable(sdd->clk);
  971. sdd->cur_speed = 0; /* Output Clock is stopped */
  972. return 0;
  973. }
  974. static int s3c64xx_spi_resume(struct device *dev)
  975. {
  976. struct platform_device *pdev = to_platform_device(dev);
  977. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  978. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  979. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  980. sci->cfg_gpio(pdev);
  981. /* Enable the clock */
  982. clk_enable(sdd->src_clk);
  983. clk_enable(sdd->clk);
  984. s3c64xx_spi_hwinit(sdd, pdev->id);
  985. spi_master_resume(master);
  986. return 0;
  987. }
  988. #endif /* CONFIG_PM */
  989. #ifdef CONFIG_PM_RUNTIME
  990. static int s3c64xx_spi_runtime_suspend(struct device *dev)
  991. {
  992. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  993. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  994. clk_disable(sdd->clk);
  995. clk_disable(sdd->src_clk);
  996. return 0;
  997. }
  998. static int s3c64xx_spi_runtime_resume(struct device *dev)
  999. {
  1000. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1001. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1002. clk_enable(sdd->src_clk);
  1003. clk_enable(sdd->clk);
  1004. return 0;
  1005. }
  1006. #endif /* CONFIG_PM_RUNTIME */
  1007. static const struct dev_pm_ops s3c64xx_spi_pm = {
  1008. SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
  1009. SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
  1010. s3c64xx_spi_runtime_resume, NULL)
  1011. };
  1012. static struct platform_driver s3c64xx_spi_driver = {
  1013. .driver = {
  1014. .name = "s3c64xx-spi",
  1015. .owner = THIS_MODULE,
  1016. .pm = &s3c64xx_spi_pm,
  1017. },
  1018. .remove = s3c64xx_spi_remove,
  1019. };
  1020. MODULE_ALIAS("platform:s3c64xx-spi");
  1021. static int __init s3c64xx_spi_init(void)
  1022. {
  1023. return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
  1024. }
  1025. subsys_initcall(s3c64xx_spi_init);
  1026. static void __exit s3c64xx_spi_exit(void)
  1027. {
  1028. platform_driver_unregister(&s3c64xx_spi_driver);
  1029. }
  1030. module_exit(s3c64xx_spi_exit);
  1031. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  1032. MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
  1033. MODULE_LICENSE("GPL");