spi_s3c64xx.c 32 KB

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