spi-s3c64xx.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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 <linux/gpio.h>
  30. #include <linux/of.h>
  31. #include <linux/of_gpio.h>
  32. #include <mach/dma.h>
  33. #include <plat/s3c64xx-spi.h>
  34. #define MAX_SPI_PORTS 3
  35. /* Registers and bit-fields */
  36. #define S3C64XX_SPI_CH_CFG 0x00
  37. #define S3C64XX_SPI_CLK_CFG 0x04
  38. #define S3C64XX_SPI_MODE_CFG 0x08
  39. #define S3C64XX_SPI_SLAVE_SEL 0x0C
  40. #define S3C64XX_SPI_INT_EN 0x10
  41. #define S3C64XX_SPI_STATUS 0x14
  42. #define S3C64XX_SPI_TX_DATA 0x18
  43. #define S3C64XX_SPI_RX_DATA 0x1C
  44. #define S3C64XX_SPI_PACKET_CNT 0x20
  45. #define S3C64XX_SPI_PENDING_CLR 0x24
  46. #define S3C64XX_SPI_SWAP_CFG 0x28
  47. #define S3C64XX_SPI_FB_CLK 0x2C
  48. #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
  49. #define S3C64XX_SPI_CH_SW_RST (1<<5)
  50. #define S3C64XX_SPI_CH_SLAVE (1<<4)
  51. #define S3C64XX_SPI_CPOL_L (1<<3)
  52. #define S3C64XX_SPI_CPHA_B (1<<2)
  53. #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
  54. #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
  55. #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
  56. #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
  57. #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
  58. #define S3C64XX_SPI_PSR_MASK 0xff
  59. #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
  60. #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
  61. #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
  62. #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
  63. #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
  64. #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
  65. #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
  66. #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
  67. #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
  68. #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
  69. #define S3C64XX_SPI_MODE_4BURST (1<<0)
  70. #define S3C64XX_SPI_SLAVE_AUTO (1<<1)
  71. #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
  72. #define S3C64XX_SPI_ACT(c) writel(0, (c)->regs + S3C64XX_SPI_SLAVE_SEL)
  73. #define S3C64XX_SPI_DEACT(c) writel(S3C64XX_SPI_SLAVE_SIG_INACT, \
  74. (c)->regs + S3C64XX_SPI_SLAVE_SEL)
  75. #define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
  76. #define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
  77. #define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
  78. #define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
  79. #define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
  80. #define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
  81. #define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
  82. #define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
  83. #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
  84. #define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
  85. #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
  86. #define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
  87. #define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
  88. #define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
  89. #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
  90. #define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
  91. #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
  92. #define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
  93. #define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
  94. #define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
  95. #define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
  96. #define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
  97. #define S3C64XX_SPI_SWAP_RX_EN (1<<4)
  98. #define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
  99. #define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
  100. #define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
  101. #define S3C64XX_SPI_SWAP_TX_EN (1<<0)
  102. #define S3C64XX_SPI_FBCLK_MSK (3<<0)
  103. #define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
  104. #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
  105. (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
  106. #define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
  107. #define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
  108. FIFO_LVL_MASK(i))
  109. #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
  110. #define S3C64XX_SPI_TRAILCNT_OFF 19
  111. #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
  112. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  113. #define RXBUSY (1<<2)
  114. #define TXBUSY (1<<3)
  115. struct s3c64xx_spi_dma_data {
  116. unsigned ch;
  117. enum dma_data_direction direction;
  118. enum dma_ch dmach;
  119. struct property *dma_prop;
  120. };
  121. /**
  122. * struct s3c64xx_spi_info - SPI Controller hardware info
  123. * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
  124. * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
  125. * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
  126. * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
  127. * @clk_from_cmu: True, if the controller does not include a clock mux and
  128. * prescaler unit.
  129. *
  130. * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
  131. * differ in some aspects such as the size of the fifo and spi bus clock
  132. * setup. Such differences are specified to the driver using this structure
  133. * which is provided as driver data to the driver.
  134. */
  135. struct s3c64xx_spi_port_config {
  136. int fifo_lvl_mask[MAX_SPI_PORTS];
  137. int rx_lvl_offset;
  138. int tx_st_done;
  139. bool high_speed;
  140. bool clk_from_cmu;
  141. };
  142. /**
  143. * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
  144. * @clk: Pointer to the spi clock.
  145. * @src_clk: Pointer to the clock used to generate SPI signals.
  146. * @master: Pointer to the SPI Protocol master.
  147. * @cntrlr_info: Platform specific data for the controller this driver manages.
  148. * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
  149. * @queue: To log SPI xfer requests.
  150. * @lock: Controller specific lock.
  151. * @state: Set of FLAGS to indicate status.
  152. * @rx_dmach: Controller's DMA channel for Rx.
  153. * @tx_dmach: Controller's DMA channel for Tx.
  154. * @sfr_start: BUS address of SPI controller regs.
  155. * @regs: Pointer to ioremap'ed controller registers.
  156. * @irq: interrupt
  157. * @xfer_completion: To indicate completion of xfer task.
  158. * @cur_mode: Stores the active configuration of the controller.
  159. * @cur_bpw: Stores the active bits per word settings.
  160. * @cur_speed: Stores the active xfer clock speed.
  161. */
  162. struct s3c64xx_spi_driver_data {
  163. void __iomem *regs;
  164. struct clk *clk;
  165. struct clk *src_clk;
  166. struct platform_device *pdev;
  167. struct spi_master *master;
  168. struct s3c64xx_spi_info *cntrlr_info;
  169. struct spi_device *tgl_spi;
  170. struct list_head queue;
  171. spinlock_t lock;
  172. unsigned long sfr_start;
  173. struct completion xfer_completion;
  174. unsigned state;
  175. unsigned cur_mode, cur_bpw;
  176. unsigned cur_speed;
  177. struct s3c64xx_spi_dma_data rx_dma;
  178. struct s3c64xx_spi_dma_data tx_dma;
  179. struct samsung_dma_ops *ops;
  180. struct s3c64xx_spi_port_config *port_conf;
  181. unsigned int port_id;
  182. unsigned long gpios[4];
  183. };
  184. static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
  185. .name = "samsung-spi-dma",
  186. };
  187. static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
  188. {
  189. void __iomem *regs = sdd->regs;
  190. unsigned long loops;
  191. u32 val;
  192. writel(0, regs + S3C64XX_SPI_PACKET_CNT);
  193. val = readl(regs + S3C64XX_SPI_CH_CFG);
  194. val |= S3C64XX_SPI_CH_SW_RST;
  195. val &= ~S3C64XX_SPI_CH_HS_EN;
  196. writel(val, regs + S3C64XX_SPI_CH_CFG);
  197. /* Flush TxFIFO*/
  198. loops = msecs_to_loops(1);
  199. do {
  200. val = readl(regs + S3C64XX_SPI_STATUS);
  201. } while (TX_FIFO_LVL(val, sdd) && loops--);
  202. if (loops == 0)
  203. dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
  204. /* Flush RxFIFO*/
  205. loops = msecs_to_loops(1);
  206. do {
  207. val = readl(regs + S3C64XX_SPI_STATUS);
  208. if (RX_FIFO_LVL(val, sdd))
  209. readl(regs + S3C64XX_SPI_RX_DATA);
  210. else
  211. break;
  212. } while (loops--);
  213. if (loops == 0)
  214. dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
  215. val = readl(regs + S3C64XX_SPI_CH_CFG);
  216. val &= ~S3C64XX_SPI_CH_SW_RST;
  217. writel(val, regs + S3C64XX_SPI_CH_CFG);
  218. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  219. val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
  220. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  221. val = readl(regs + S3C64XX_SPI_CH_CFG);
  222. val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
  223. writel(val, regs + S3C64XX_SPI_CH_CFG);
  224. }
  225. static void s3c64xx_spi_dmacb(void *data)
  226. {
  227. struct s3c64xx_spi_driver_data *sdd;
  228. struct s3c64xx_spi_dma_data *dma = data;
  229. unsigned long flags;
  230. if (dma->direction == DMA_DEV_TO_MEM)
  231. sdd = container_of(data,
  232. struct s3c64xx_spi_driver_data, rx_dma);
  233. else
  234. sdd = container_of(data,
  235. struct s3c64xx_spi_driver_data, tx_dma);
  236. spin_lock_irqsave(&sdd->lock, flags);
  237. if (dma->direction == DMA_DEV_TO_MEM) {
  238. sdd->state &= ~RXBUSY;
  239. if (!(sdd->state & TXBUSY))
  240. complete(&sdd->xfer_completion);
  241. } else {
  242. sdd->state &= ~TXBUSY;
  243. if (!(sdd->state & RXBUSY))
  244. complete(&sdd->xfer_completion);
  245. }
  246. spin_unlock_irqrestore(&sdd->lock, flags);
  247. }
  248. static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
  249. unsigned len, dma_addr_t buf)
  250. {
  251. struct s3c64xx_spi_driver_data *sdd;
  252. struct samsung_dma_prep info;
  253. struct samsung_dma_config config;
  254. if (dma->direction == DMA_DEV_TO_MEM) {
  255. sdd = container_of((void *)dma,
  256. struct s3c64xx_spi_driver_data, rx_dma);
  257. config.direction = sdd->rx_dma.direction;
  258. config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
  259. config.width = sdd->cur_bpw / 8;
  260. sdd->ops->config(sdd->rx_dma.ch, &config);
  261. } else {
  262. sdd = container_of((void *)dma,
  263. struct s3c64xx_spi_driver_data, tx_dma);
  264. config.direction = sdd->tx_dma.direction;
  265. config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
  266. config.width = sdd->cur_bpw / 8;
  267. sdd->ops->config(sdd->tx_dma.ch, &config);
  268. }
  269. info.cap = DMA_SLAVE;
  270. info.len = len;
  271. info.fp = s3c64xx_spi_dmacb;
  272. info.fp_param = dma;
  273. info.direction = dma->direction;
  274. info.buf = buf;
  275. sdd->ops->prepare(dma->ch, &info);
  276. sdd->ops->trigger(dma->ch);
  277. }
  278. static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
  279. {
  280. struct samsung_dma_req req;
  281. sdd->ops = samsung_dma_get_ops();
  282. req.cap = DMA_SLAVE;
  283. req.client = &s3c64xx_spi_dma_client;
  284. req.dt_dmach_prop = sdd->rx_dma.dma_prop;
  285. sdd->rx_dma.ch = sdd->ops->request(sdd->rx_dma.dmach, &req);
  286. req.dt_dmach_prop = sdd->tx_dma.dma_prop;
  287. sdd->tx_dma.ch = sdd->ops->request(sdd->tx_dma.dmach, &req);
  288. return 1;
  289. }
  290. static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
  291. struct spi_device *spi,
  292. struct spi_transfer *xfer, int dma_mode)
  293. {
  294. void __iomem *regs = sdd->regs;
  295. u32 modecfg, chcfg;
  296. modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
  297. modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
  298. chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
  299. chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
  300. if (dma_mode) {
  301. chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
  302. } else {
  303. /* Always shift in data in FIFO, even if xfer is Tx only,
  304. * this helps setting PCKT_CNT value for generating clocks
  305. * as exactly needed.
  306. */
  307. chcfg |= S3C64XX_SPI_CH_RXCH_ON;
  308. writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
  309. | S3C64XX_SPI_PACKET_CNT_EN,
  310. regs + S3C64XX_SPI_PACKET_CNT);
  311. }
  312. if (xfer->tx_buf != NULL) {
  313. sdd->state |= TXBUSY;
  314. chcfg |= S3C64XX_SPI_CH_TXCH_ON;
  315. if (dma_mode) {
  316. modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
  317. prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
  318. } else {
  319. switch (sdd->cur_bpw) {
  320. case 32:
  321. iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
  322. xfer->tx_buf, xfer->len / 4);
  323. break;
  324. case 16:
  325. iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
  326. xfer->tx_buf, xfer->len / 2);
  327. break;
  328. default:
  329. iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
  330. xfer->tx_buf, xfer->len);
  331. break;
  332. }
  333. }
  334. }
  335. if (xfer->rx_buf != NULL) {
  336. sdd->state |= RXBUSY;
  337. if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
  338. && !(sdd->cur_mode & SPI_CPHA))
  339. chcfg |= S3C64XX_SPI_CH_HS_EN;
  340. if (dma_mode) {
  341. modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
  342. chcfg |= S3C64XX_SPI_CH_RXCH_ON;
  343. writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
  344. | S3C64XX_SPI_PACKET_CNT_EN,
  345. regs + S3C64XX_SPI_PACKET_CNT);
  346. prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
  347. }
  348. }
  349. writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
  350. writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
  351. }
  352. static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
  353. struct spi_device *spi)
  354. {
  355. struct s3c64xx_spi_csinfo *cs;
  356. if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
  357. if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
  358. /* Deselect the last toggled device */
  359. cs = sdd->tgl_spi->controller_data;
  360. gpio_set_value(cs->line,
  361. spi->mode & SPI_CS_HIGH ? 0 : 1);
  362. }
  363. sdd->tgl_spi = NULL;
  364. }
  365. cs = spi->controller_data;
  366. gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
  367. }
  368. static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
  369. struct spi_transfer *xfer, int dma_mode)
  370. {
  371. void __iomem *regs = sdd->regs;
  372. unsigned long val;
  373. int ms;
  374. /* millisecs to xfer 'len' bytes @ 'cur_speed' */
  375. ms = xfer->len * 8 * 1000 / sdd->cur_speed;
  376. ms += 10; /* some tolerance */
  377. if (dma_mode) {
  378. val = msecs_to_jiffies(ms) + 10;
  379. val = wait_for_completion_timeout(&sdd->xfer_completion, val);
  380. } else {
  381. u32 status;
  382. val = msecs_to_loops(ms);
  383. do {
  384. status = readl(regs + S3C64XX_SPI_STATUS);
  385. } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
  386. }
  387. if (!val)
  388. return -EIO;
  389. if (dma_mode) {
  390. u32 status;
  391. /*
  392. * DmaTx returns after simply writing data in the FIFO,
  393. * w/o waiting for real transmission on the bus to finish.
  394. * DmaRx returns only after Dma read data from FIFO which
  395. * needs bus transmission to finish, so we don't worry if
  396. * Xfer involved Rx(with or without Tx).
  397. */
  398. if (xfer->rx_buf == NULL) {
  399. val = msecs_to_loops(10);
  400. status = readl(regs + S3C64XX_SPI_STATUS);
  401. while ((TX_FIFO_LVL(status, sdd)
  402. || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
  403. && --val) {
  404. cpu_relax();
  405. status = readl(regs + S3C64XX_SPI_STATUS);
  406. }
  407. if (!val)
  408. return -EIO;
  409. }
  410. } else {
  411. /* If it was only Tx */
  412. if (xfer->rx_buf == NULL) {
  413. sdd->state &= ~TXBUSY;
  414. return 0;
  415. }
  416. switch (sdd->cur_bpw) {
  417. case 32:
  418. ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
  419. xfer->rx_buf, xfer->len / 4);
  420. break;
  421. case 16:
  422. ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
  423. xfer->rx_buf, xfer->len / 2);
  424. break;
  425. default:
  426. ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
  427. xfer->rx_buf, xfer->len);
  428. break;
  429. }
  430. sdd->state &= ~RXBUSY;
  431. }
  432. return 0;
  433. }
  434. static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
  435. struct spi_device *spi)
  436. {
  437. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  438. if (sdd->tgl_spi == spi)
  439. sdd->tgl_spi = NULL;
  440. gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
  441. }
  442. static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
  443. {
  444. void __iomem *regs = sdd->regs;
  445. u32 val;
  446. /* Disable Clock */
  447. if (sdd->port_conf->clk_from_cmu) {
  448. clk_disable(sdd->src_clk);
  449. } else {
  450. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  451. val &= ~S3C64XX_SPI_ENCLK_ENABLE;
  452. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  453. }
  454. /* Set Polarity and Phase */
  455. val = readl(regs + S3C64XX_SPI_CH_CFG);
  456. val &= ~(S3C64XX_SPI_CH_SLAVE |
  457. S3C64XX_SPI_CPOL_L |
  458. S3C64XX_SPI_CPHA_B);
  459. if (sdd->cur_mode & SPI_CPOL)
  460. val |= S3C64XX_SPI_CPOL_L;
  461. if (sdd->cur_mode & SPI_CPHA)
  462. val |= S3C64XX_SPI_CPHA_B;
  463. writel(val, regs + S3C64XX_SPI_CH_CFG);
  464. /* Set Channel & DMA Mode */
  465. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  466. val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
  467. | S3C64XX_SPI_MODE_CH_TSZ_MASK);
  468. switch (sdd->cur_bpw) {
  469. case 32:
  470. val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
  471. val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
  472. break;
  473. case 16:
  474. val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
  475. val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
  476. break;
  477. default:
  478. val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
  479. val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
  480. break;
  481. }
  482. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  483. if (sdd->port_conf->clk_from_cmu) {
  484. /* Configure Clock */
  485. /* There is half-multiplier before the SPI */
  486. clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
  487. /* Enable Clock */
  488. clk_enable(sdd->src_clk);
  489. } else {
  490. /* Configure Clock */
  491. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  492. val &= ~S3C64XX_SPI_PSR_MASK;
  493. val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
  494. & S3C64XX_SPI_PSR_MASK);
  495. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  496. /* Enable Clock */
  497. val = readl(regs + S3C64XX_SPI_CLK_CFG);
  498. val |= S3C64XX_SPI_ENCLK_ENABLE;
  499. writel(val, regs + S3C64XX_SPI_CLK_CFG);
  500. }
  501. }
  502. #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
  503. static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
  504. struct spi_message *msg)
  505. {
  506. struct device *dev = &sdd->pdev->dev;
  507. struct spi_transfer *xfer;
  508. if (msg->is_dma_mapped)
  509. return 0;
  510. /* First mark all xfer unmapped */
  511. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  512. xfer->rx_dma = XFER_DMAADDR_INVALID;
  513. xfer->tx_dma = XFER_DMAADDR_INVALID;
  514. }
  515. /* Map until end or first fail */
  516. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  517. if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
  518. continue;
  519. if (xfer->tx_buf != NULL) {
  520. xfer->tx_dma = dma_map_single(dev,
  521. (void *)xfer->tx_buf, xfer->len,
  522. DMA_TO_DEVICE);
  523. if (dma_mapping_error(dev, xfer->tx_dma)) {
  524. dev_err(dev, "dma_map_single Tx failed\n");
  525. xfer->tx_dma = XFER_DMAADDR_INVALID;
  526. return -ENOMEM;
  527. }
  528. }
  529. if (xfer->rx_buf != NULL) {
  530. xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
  531. xfer->len, DMA_FROM_DEVICE);
  532. if (dma_mapping_error(dev, xfer->rx_dma)) {
  533. dev_err(dev, "dma_map_single Rx failed\n");
  534. dma_unmap_single(dev, xfer->tx_dma,
  535. xfer->len, DMA_TO_DEVICE);
  536. xfer->tx_dma = XFER_DMAADDR_INVALID;
  537. xfer->rx_dma = XFER_DMAADDR_INVALID;
  538. return -ENOMEM;
  539. }
  540. }
  541. }
  542. return 0;
  543. }
  544. static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
  545. struct spi_message *msg)
  546. {
  547. struct device *dev = &sdd->pdev->dev;
  548. struct spi_transfer *xfer;
  549. if (msg->is_dma_mapped)
  550. return;
  551. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  552. if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
  553. continue;
  554. if (xfer->rx_buf != NULL
  555. && xfer->rx_dma != XFER_DMAADDR_INVALID)
  556. dma_unmap_single(dev, xfer->rx_dma,
  557. xfer->len, DMA_FROM_DEVICE);
  558. if (xfer->tx_buf != NULL
  559. && xfer->tx_dma != XFER_DMAADDR_INVALID)
  560. dma_unmap_single(dev, xfer->tx_dma,
  561. xfer->len, DMA_TO_DEVICE);
  562. }
  563. }
  564. static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
  565. struct spi_message *msg)
  566. {
  567. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  568. struct spi_device *spi = msg->spi;
  569. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  570. struct spi_transfer *xfer;
  571. int status = 0, cs_toggle = 0;
  572. u32 speed;
  573. u8 bpw;
  574. /* If Master's(controller) state differs from that needed by Slave */
  575. if (sdd->cur_speed != spi->max_speed_hz
  576. || sdd->cur_mode != spi->mode
  577. || sdd->cur_bpw != spi->bits_per_word) {
  578. sdd->cur_bpw = spi->bits_per_word;
  579. sdd->cur_speed = spi->max_speed_hz;
  580. sdd->cur_mode = spi->mode;
  581. s3c64xx_spi_config(sdd);
  582. }
  583. /* Map all the transfers if needed */
  584. if (s3c64xx_spi_map_mssg(sdd, msg)) {
  585. dev_err(&spi->dev,
  586. "Xfer: Unable to map message buffers!\n");
  587. status = -ENOMEM;
  588. goto out;
  589. }
  590. /* Configure feedback delay */
  591. writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
  592. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  593. unsigned long flags;
  594. int use_dma;
  595. INIT_COMPLETION(sdd->xfer_completion);
  596. /* Only BPW and Speed may change across transfers */
  597. bpw = xfer->bits_per_word ? : spi->bits_per_word;
  598. speed = xfer->speed_hz ? : spi->max_speed_hz;
  599. if (xfer->len % (bpw / 8)) {
  600. dev_err(&spi->dev,
  601. "Xfer length(%u) not a multiple of word size(%u)\n",
  602. xfer->len, bpw / 8);
  603. status = -EIO;
  604. goto out;
  605. }
  606. if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
  607. sdd->cur_bpw = bpw;
  608. sdd->cur_speed = speed;
  609. s3c64xx_spi_config(sdd);
  610. }
  611. /* Polling method for xfers not bigger than FIFO capacity */
  612. if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
  613. use_dma = 0;
  614. else
  615. use_dma = 1;
  616. spin_lock_irqsave(&sdd->lock, flags);
  617. /* Pending only which is to be done */
  618. sdd->state &= ~RXBUSY;
  619. sdd->state &= ~TXBUSY;
  620. enable_datapath(sdd, spi, xfer, use_dma);
  621. /* Slave Select */
  622. enable_cs(sdd, spi);
  623. /* Start the signals */
  624. S3C64XX_SPI_ACT(sdd);
  625. spin_unlock_irqrestore(&sdd->lock, flags);
  626. status = wait_for_xfer(sdd, xfer, use_dma);
  627. /* Quiese the signals */
  628. S3C64XX_SPI_DEACT(sdd);
  629. if (status) {
  630. dev_err(&spi->dev, "I/O Error: "
  631. "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
  632. xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
  633. (sdd->state & RXBUSY) ? 'f' : 'p',
  634. (sdd->state & TXBUSY) ? 'f' : 'p',
  635. xfer->len);
  636. if (use_dma) {
  637. if (xfer->tx_buf != NULL
  638. && (sdd->state & TXBUSY))
  639. sdd->ops->stop(sdd->tx_dma.ch);
  640. if (xfer->rx_buf != NULL
  641. && (sdd->state & RXBUSY))
  642. sdd->ops->stop(sdd->rx_dma.ch);
  643. }
  644. goto out;
  645. }
  646. if (xfer->delay_usecs)
  647. udelay(xfer->delay_usecs);
  648. if (xfer->cs_change) {
  649. /* Hint that the next mssg is gonna be
  650. for the same device */
  651. if (list_is_last(&xfer->transfer_list,
  652. &msg->transfers))
  653. cs_toggle = 1;
  654. else
  655. disable_cs(sdd, spi);
  656. }
  657. msg->actual_length += xfer->len;
  658. flush_fifo(sdd);
  659. }
  660. out:
  661. if (!cs_toggle || status)
  662. disable_cs(sdd, spi);
  663. else
  664. sdd->tgl_spi = spi;
  665. s3c64xx_spi_unmap_mssg(sdd, msg);
  666. msg->status = status;
  667. spi_finalize_current_message(master);
  668. return 0;
  669. }
  670. static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
  671. {
  672. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  673. /* Acquire DMA channels */
  674. while (!acquire_dma(sdd))
  675. msleep(10);
  676. pm_runtime_get_sync(&sdd->pdev->dev);
  677. return 0;
  678. }
  679. static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
  680. {
  681. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  682. /* Free DMA channels */
  683. sdd->ops->release(sdd->rx_dma.ch, &s3c64xx_spi_dma_client);
  684. sdd->ops->release(sdd->tx_dma.ch, &s3c64xx_spi_dma_client);
  685. pm_runtime_put(&sdd->pdev->dev);
  686. return 0;
  687. }
  688. static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
  689. struct s3c64xx_spi_driver_data *sdd,
  690. struct spi_device *spi)
  691. {
  692. struct s3c64xx_spi_csinfo *cs;
  693. struct device_node *slave_np, *data_np;
  694. u32 fb_delay = 0;
  695. slave_np = spi->dev.of_node;
  696. if (!slave_np) {
  697. dev_err(&spi->dev, "device node not found\n");
  698. return ERR_PTR(-EINVAL);
  699. }
  700. for_each_child_of_node(slave_np, data_np)
  701. if (!strcmp(data_np->name, "controller-data"))
  702. break;
  703. if (!data_np) {
  704. dev_err(&spi->dev, "child node 'controller-data' not found\n");
  705. return ERR_PTR(-EINVAL);
  706. }
  707. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  708. if (!cs) {
  709. dev_err(&spi->dev, "could not allocate memory for controller"
  710. " data\n");
  711. return ERR_PTR(-ENOMEM);
  712. }
  713. cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
  714. if (!gpio_is_valid(cs->line)) {
  715. dev_err(&spi->dev, "chip select gpio is not specified or "
  716. "invalid\n");
  717. kfree(cs);
  718. return ERR_PTR(-EINVAL);
  719. }
  720. of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
  721. cs->fb_delay = fb_delay;
  722. return cs;
  723. }
  724. /*
  725. * Here we only check the validity of requested configuration
  726. * and save the configuration in a local data-structure.
  727. * The controller is actually configured only just before we
  728. * get a message to transfer.
  729. */
  730. static int s3c64xx_spi_setup(struct spi_device *spi)
  731. {
  732. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  733. struct s3c64xx_spi_driver_data *sdd;
  734. struct s3c64xx_spi_info *sci;
  735. struct spi_message *msg;
  736. unsigned long flags;
  737. int err;
  738. sdd = spi_master_get_devdata(spi->master);
  739. if (!cs && spi->dev.of_node) {
  740. cs = s3c64xx_get_slave_ctrldata(sdd, spi);
  741. spi->controller_data = cs;
  742. }
  743. if (IS_ERR_OR_NULL(cs)) {
  744. dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
  745. return -ENODEV;
  746. }
  747. if (!spi_get_ctldata(spi)) {
  748. err = gpio_request(cs->line, dev_name(&spi->dev));
  749. if (err) {
  750. dev_err(&spi->dev,
  751. "Failed to get /CS gpio [%d]: %d\n",
  752. cs->line, err);
  753. goto err_gpio_req;
  754. }
  755. spi_set_ctldata(spi, cs);
  756. }
  757. sci = sdd->cntrlr_info;
  758. spin_lock_irqsave(&sdd->lock, flags);
  759. list_for_each_entry(msg, &sdd->queue, queue) {
  760. /* Is some mssg is already queued for this device */
  761. if (msg->spi == spi) {
  762. dev_err(&spi->dev,
  763. "setup: attempt while mssg in queue!\n");
  764. spin_unlock_irqrestore(&sdd->lock, flags);
  765. err = -EBUSY;
  766. goto err_msgq;
  767. }
  768. }
  769. spin_unlock_irqrestore(&sdd->lock, flags);
  770. if (spi->bits_per_word != 8
  771. && spi->bits_per_word != 16
  772. && spi->bits_per_word != 32) {
  773. dev_err(&spi->dev, "setup: %dbits/wrd not supported!\n",
  774. spi->bits_per_word);
  775. err = -EINVAL;
  776. goto setup_exit;
  777. }
  778. pm_runtime_get_sync(&sdd->pdev->dev);
  779. /* Check if we can provide the requested rate */
  780. if (!sdd->port_conf->clk_from_cmu) {
  781. u32 psr, speed;
  782. /* Max possible */
  783. speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
  784. if (spi->max_speed_hz > speed)
  785. spi->max_speed_hz = speed;
  786. psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
  787. psr &= S3C64XX_SPI_PSR_MASK;
  788. if (psr == S3C64XX_SPI_PSR_MASK)
  789. psr--;
  790. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  791. if (spi->max_speed_hz < speed) {
  792. if (psr+1 < S3C64XX_SPI_PSR_MASK) {
  793. psr++;
  794. } else {
  795. err = -EINVAL;
  796. goto setup_exit;
  797. }
  798. }
  799. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  800. if (spi->max_speed_hz >= speed) {
  801. spi->max_speed_hz = speed;
  802. } else {
  803. err = -EINVAL;
  804. goto setup_exit;
  805. }
  806. }
  807. pm_runtime_put(&sdd->pdev->dev);
  808. disable_cs(sdd, spi);
  809. return 0;
  810. setup_exit:
  811. /* setup() returns with device de-selected */
  812. disable_cs(sdd, spi);
  813. err_msgq:
  814. gpio_free(cs->line);
  815. spi_set_ctldata(spi, NULL);
  816. err_gpio_req:
  817. kfree(cs);
  818. return err;
  819. }
  820. static void s3c64xx_spi_cleanup(struct spi_device *spi)
  821. {
  822. struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
  823. if (cs) {
  824. gpio_free(cs->line);
  825. if (spi->dev.of_node)
  826. kfree(cs);
  827. }
  828. spi_set_ctldata(spi, NULL);
  829. }
  830. static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
  831. {
  832. struct s3c64xx_spi_driver_data *sdd = data;
  833. struct spi_master *spi = sdd->master;
  834. unsigned int val;
  835. val = readl(sdd->regs + S3C64XX_SPI_PENDING_CLR);
  836. val &= S3C64XX_SPI_PND_RX_OVERRUN_CLR |
  837. S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
  838. S3C64XX_SPI_PND_TX_OVERRUN_CLR |
  839. S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
  840. writel(val, sdd->regs + S3C64XX_SPI_PENDING_CLR);
  841. if (val & S3C64XX_SPI_PND_RX_OVERRUN_CLR)
  842. dev_err(&spi->dev, "RX overrun\n");
  843. if (val & S3C64XX_SPI_PND_RX_UNDERRUN_CLR)
  844. dev_err(&spi->dev, "RX underrun\n");
  845. if (val & S3C64XX_SPI_PND_TX_OVERRUN_CLR)
  846. dev_err(&spi->dev, "TX overrun\n");
  847. if (val & S3C64XX_SPI_PND_TX_UNDERRUN_CLR)
  848. dev_err(&spi->dev, "TX underrun\n");
  849. return IRQ_HANDLED;
  850. }
  851. static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
  852. {
  853. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  854. void __iomem *regs = sdd->regs;
  855. unsigned int val;
  856. sdd->cur_speed = 0;
  857. S3C64XX_SPI_DEACT(sdd);
  858. /* Disable Interrupts - we use Polling if not DMA mode */
  859. writel(0, regs + S3C64XX_SPI_INT_EN);
  860. if (!sdd->port_conf->clk_from_cmu)
  861. writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
  862. regs + S3C64XX_SPI_CLK_CFG);
  863. writel(0, regs + S3C64XX_SPI_MODE_CFG);
  864. writel(0, regs + S3C64XX_SPI_PACKET_CNT);
  865. /* Clear any irq pending bits */
  866. writel(readl(regs + S3C64XX_SPI_PENDING_CLR),
  867. regs + S3C64XX_SPI_PENDING_CLR);
  868. writel(0, regs + S3C64XX_SPI_SWAP_CFG);
  869. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  870. val &= ~S3C64XX_SPI_MODE_4BURST;
  871. val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  872. val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  873. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  874. flush_fifo(sdd);
  875. }
  876. static int __devinit s3c64xx_spi_get_dmares(
  877. struct s3c64xx_spi_driver_data *sdd, bool tx)
  878. {
  879. struct platform_device *pdev = sdd->pdev;
  880. struct s3c64xx_spi_dma_data *dma_data;
  881. struct property *prop;
  882. struct resource *res;
  883. char prop_name[15], *chan_str;
  884. if (tx) {
  885. dma_data = &sdd->tx_dma;
  886. dma_data->direction = DMA_TO_DEVICE;
  887. chan_str = "tx";
  888. } else {
  889. dma_data = &sdd->rx_dma;
  890. dma_data->direction = DMA_FROM_DEVICE;
  891. chan_str = "rx";
  892. }
  893. if (!sdd->pdev->dev.of_node) {
  894. res = platform_get_resource(pdev, IORESOURCE_DMA, tx ? 0 : 1);
  895. if (!res) {
  896. dev_err(&pdev->dev, "Unable to get SPI-%s dma "
  897. "resource\n", chan_str);
  898. return -ENXIO;
  899. }
  900. dma_data->dmach = res->start;
  901. return 0;
  902. }
  903. sprintf(prop_name, "%s-dma-channel", chan_str);
  904. prop = of_find_property(pdev->dev.of_node, prop_name, NULL);
  905. if (!prop) {
  906. dev_err(&pdev->dev, "%s dma channel property not specified\n",
  907. chan_str);
  908. return -ENXIO;
  909. }
  910. dma_data->dmach = DMACH_DT_PROP;
  911. dma_data->dma_prop = prop;
  912. return 0;
  913. }
  914. #ifdef CONFIG_OF
  915. static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
  916. {
  917. struct device *dev = &sdd->pdev->dev;
  918. int idx, gpio, ret;
  919. /* find gpios for mosi, miso and clock lines */
  920. for (idx = 0; idx < 3; idx++) {
  921. gpio = of_get_gpio(dev->of_node, idx);
  922. if (!gpio_is_valid(gpio)) {
  923. dev_err(dev, "invalid gpio[%d]: %d\n", idx, gpio);
  924. goto free_gpio;
  925. }
  926. ret = gpio_request(gpio, "spi-bus");
  927. if (ret) {
  928. dev_err(dev, "gpio [%d] request failed: %d\n",
  929. gpio, ret);
  930. goto free_gpio;
  931. }
  932. }
  933. return 0;
  934. free_gpio:
  935. while (--idx >= 0)
  936. gpio_free(sdd->gpios[idx]);
  937. return -EINVAL;
  938. }
  939. static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
  940. {
  941. unsigned int idx;
  942. for (idx = 0; idx < 3; idx++)
  943. gpio_free(sdd->gpios[idx]);
  944. }
  945. static struct __devinit s3c64xx_spi_info * s3c64xx_spi_parse_dt(
  946. struct device *dev)
  947. {
  948. struct s3c64xx_spi_info *sci;
  949. u32 temp;
  950. sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
  951. if (!sci) {
  952. dev_err(dev, "memory allocation for spi_info failed\n");
  953. return ERR_PTR(-ENOMEM);
  954. }
  955. if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
  956. dev_warn(dev, "spi bus clock parent not specified, using "
  957. "clock at index 0 as parent\n");
  958. sci->src_clk_nr = 0;
  959. } else {
  960. sci->src_clk_nr = temp;
  961. }
  962. if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
  963. dev_warn(dev, "number of chip select lines not specified, "
  964. "assuming 1 chip select line\n");
  965. sci->num_cs = 1;
  966. } else {
  967. sci->num_cs = temp;
  968. }
  969. return sci;
  970. }
  971. #else
  972. static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
  973. {
  974. return dev->platform_data;
  975. }
  976. static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
  977. {
  978. return -EINVAL;
  979. }
  980. static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
  981. {
  982. }
  983. #endif
  984. static const struct of_device_id s3c64xx_spi_dt_match[];
  985. static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
  986. struct platform_device *pdev)
  987. {
  988. #ifdef CONFIG_OF
  989. if (pdev->dev.of_node) {
  990. const struct of_device_id *match;
  991. match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
  992. return (struct s3c64xx_spi_port_config *)match->data;
  993. }
  994. #endif
  995. return (struct s3c64xx_spi_port_config *)
  996. platform_get_device_id(pdev)->driver_data;
  997. }
  998. static int __init s3c64xx_spi_probe(struct platform_device *pdev)
  999. {
  1000. struct resource *mem_res;
  1001. struct s3c64xx_spi_driver_data *sdd;
  1002. struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
  1003. struct spi_master *master;
  1004. int ret, irq;
  1005. char clk_name[16];
  1006. if (!sci && pdev->dev.of_node) {
  1007. sci = s3c64xx_spi_parse_dt(&pdev->dev);
  1008. if (IS_ERR(sci))
  1009. return PTR_ERR(sci);
  1010. }
  1011. if (!sci) {
  1012. dev_err(&pdev->dev, "platform_data missing!\n");
  1013. return -ENODEV;
  1014. }
  1015. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1016. if (mem_res == NULL) {
  1017. dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
  1018. return -ENXIO;
  1019. }
  1020. irq = platform_get_irq(pdev, 0);
  1021. if (irq < 0) {
  1022. dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  1023. return irq;
  1024. }
  1025. master = spi_alloc_master(&pdev->dev,
  1026. sizeof(struct s3c64xx_spi_driver_data));
  1027. if (master == NULL) {
  1028. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  1029. return -ENOMEM;
  1030. }
  1031. platform_set_drvdata(pdev, master);
  1032. sdd = spi_master_get_devdata(master);
  1033. sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
  1034. sdd->master = master;
  1035. sdd->cntrlr_info = sci;
  1036. sdd->pdev = pdev;
  1037. sdd->sfr_start = mem_res->start;
  1038. if (pdev->dev.of_node) {
  1039. ret = of_alias_get_id(pdev->dev.of_node, "spi");
  1040. if (ret < 0) {
  1041. dev_err(&pdev->dev, "failed to get alias id, "
  1042. "errno %d\n", ret);
  1043. goto err0;
  1044. }
  1045. sdd->port_id = ret;
  1046. } else {
  1047. sdd->port_id = pdev->id;
  1048. }
  1049. sdd->cur_bpw = 8;
  1050. ret = s3c64xx_spi_get_dmares(sdd, true);
  1051. if (ret)
  1052. goto err0;
  1053. ret = s3c64xx_spi_get_dmares(sdd, false);
  1054. if (ret)
  1055. goto err0;
  1056. master->dev.of_node = pdev->dev.of_node;
  1057. master->bus_num = sdd->port_id;
  1058. master->setup = s3c64xx_spi_setup;
  1059. master->cleanup = s3c64xx_spi_cleanup;
  1060. master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
  1061. master->transfer_one_message = s3c64xx_spi_transfer_one_message;
  1062. master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
  1063. master->num_chipselect = sci->num_cs;
  1064. master->dma_alignment = 8;
  1065. /* the spi->mode bits understood by this driver: */
  1066. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1067. if (request_mem_region(mem_res->start,
  1068. resource_size(mem_res), pdev->name) == NULL) {
  1069. dev_err(&pdev->dev, "Req mem region failed\n");
  1070. ret = -ENXIO;
  1071. goto err0;
  1072. }
  1073. sdd->regs = ioremap(mem_res->start, resource_size(mem_res));
  1074. if (sdd->regs == NULL) {
  1075. dev_err(&pdev->dev, "Unable to remap IO\n");
  1076. ret = -ENXIO;
  1077. goto err1;
  1078. }
  1079. if (!sci->cfg_gpio && pdev->dev.of_node) {
  1080. if (s3c64xx_spi_parse_dt_gpio(sdd))
  1081. return -EBUSY;
  1082. } else if (sci->cfg_gpio == NULL || sci->cfg_gpio()) {
  1083. dev_err(&pdev->dev, "Unable to config gpio\n");
  1084. ret = -EBUSY;
  1085. goto err2;
  1086. }
  1087. /* Setup clocks */
  1088. sdd->clk = clk_get(&pdev->dev, "spi");
  1089. if (IS_ERR(sdd->clk)) {
  1090. dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
  1091. ret = PTR_ERR(sdd->clk);
  1092. goto err3;
  1093. }
  1094. if (clk_enable(sdd->clk)) {
  1095. dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
  1096. ret = -EBUSY;
  1097. goto err4;
  1098. }
  1099. sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
  1100. sdd->src_clk = clk_get(&pdev->dev, clk_name);
  1101. if (IS_ERR(sdd->src_clk)) {
  1102. dev_err(&pdev->dev,
  1103. "Unable to acquire clock '%s'\n", clk_name);
  1104. ret = PTR_ERR(sdd->src_clk);
  1105. goto err5;
  1106. }
  1107. if (clk_enable(sdd->src_clk)) {
  1108. dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
  1109. ret = -EBUSY;
  1110. goto err6;
  1111. }
  1112. /* Setup Deufult Mode */
  1113. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1114. spin_lock_init(&sdd->lock);
  1115. init_completion(&sdd->xfer_completion);
  1116. INIT_LIST_HEAD(&sdd->queue);
  1117. ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd);
  1118. if (ret != 0) {
  1119. dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
  1120. irq, ret);
  1121. goto err7;
  1122. }
  1123. writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
  1124. S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
  1125. sdd->regs + S3C64XX_SPI_INT_EN);
  1126. if (spi_register_master(master)) {
  1127. dev_err(&pdev->dev, "cannot register SPI master\n");
  1128. ret = -EBUSY;
  1129. goto err8;
  1130. }
  1131. dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
  1132. "with %d Slaves attached\n",
  1133. sdd->port_id, master->num_chipselect);
  1134. dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
  1135. mem_res->end, mem_res->start,
  1136. sdd->rx_dma.dmach, sdd->tx_dma.dmach);
  1137. pm_runtime_enable(&pdev->dev);
  1138. return 0;
  1139. err8:
  1140. free_irq(irq, sdd);
  1141. err7:
  1142. clk_disable(sdd->src_clk);
  1143. err6:
  1144. clk_put(sdd->src_clk);
  1145. err5:
  1146. clk_disable(sdd->clk);
  1147. err4:
  1148. clk_put(sdd->clk);
  1149. err3:
  1150. if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
  1151. s3c64xx_spi_dt_gpio_free(sdd);
  1152. err2:
  1153. iounmap((void *) sdd->regs);
  1154. err1:
  1155. release_mem_region(mem_res->start, resource_size(mem_res));
  1156. err0:
  1157. platform_set_drvdata(pdev, NULL);
  1158. spi_master_put(master);
  1159. return ret;
  1160. }
  1161. static int s3c64xx_spi_remove(struct platform_device *pdev)
  1162. {
  1163. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  1164. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1165. struct resource *mem_res;
  1166. pm_runtime_disable(&pdev->dev);
  1167. spi_unregister_master(master);
  1168. writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
  1169. free_irq(platform_get_irq(pdev, 0), sdd);
  1170. clk_disable(sdd->src_clk);
  1171. clk_put(sdd->src_clk);
  1172. clk_disable(sdd->clk);
  1173. clk_put(sdd->clk);
  1174. if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
  1175. s3c64xx_spi_dt_gpio_free(sdd);
  1176. iounmap((void *) sdd->regs);
  1177. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1178. if (mem_res != NULL)
  1179. release_mem_region(mem_res->start, resource_size(mem_res));
  1180. platform_set_drvdata(pdev, NULL);
  1181. spi_master_put(master);
  1182. return 0;
  1183. }
  1184. #ifdef CONFIG_PM
  1185. static int s3c64xx_spi_suspend(struct device *dev)
  1186. {
  1187. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1188. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1189. spi_master_suspend(master);
  1190. /* Disable the clock */
  1191. clk_disable(sdd->src_clk);
  1192. clk_disable(sdd->clk);
  1193. if (!sdd->cntrlr_info->cfg_gpio && dev->of_node)
  1194. s3c64xx_spi_dt_gpio_free(sdd);
  1195. sdd->cur_speed = 0; /* Output Clock is stopped */
  1196. return 0;
  1197. }
  1198. static int s3c64xx_spi_resume(struct device *dev)
  1199. {
  1200. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1201. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1202. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  1203. if (!sci->cfg_gpio && dev->of_node)
  1204. s3c64xx_spi_parse_dt_gpio(sdd);
  1205. else
  1206. sci->cfg_gpio();
  1207. /* Enable the clock */
  1208. clk_enable(sdd->src_clk);
  1209. clk_enable(sdd->clk);
  1210. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1211. spi_master_resume(master);
  1212. return 0;
  1213. }
  1214. #endif /* CONFIG_PM */
  1215. #ifdef CONFIG_PM_RUNTIME
  1216. static int s3c64xx_spi_runtime_suspend(struct device *dev)
  1217. {
  1218. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1219. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1220. clk_disable(sdd->clk);
  1221. clk_disable(sdd->src_clk);
  1222. return 0;
  1223. }
  1224. static int s3c64xx_spi_runtime_resume(struct device *dev)
  1225. {
  1226. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1227. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1228. clk_enable(sdd->src_clk);
  1229. clk_enable(sdd->clk);
  1230. return 0;
  1231. }
  1232. #endif /* CONFIG_PM_RUNTIME */
  1233. static const struct dev_pm_ops s3c64xx_spi_pm = {
  1234. SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
  1235. SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
  1236. s3c64xx_spi_runtime_resume, NULL)
  1237. };
  1238. struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
  1239. .fifo_lvl_mask = { 0x7f },
  1240. .rx_lvl_offset = 13,
  1241. .tx_st_done = 21,
  1242. .high_speed = true,
  1243. };
  1244. struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
  1245. .fifo_lvl_mask = { 0x7f, 0x7F },
  1246. .rx_lvl_offset = 13,
  1247. .tx_st_done = 21,
  1248. };
  1249. struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
  1250. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1251. .rx_lvl_offset = 15,
  1252. .tx_st_done = 25,
  1253. };
  1254. struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
  1255. .fifo_lvl_mask = { 0x7f, 0x7F },
  1256. .rx_lvl_offset = 13,
  1257. .tx_st_done = 21,
  1258. .high_speed = true,
  1259. };
  1260. struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
  1261. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1262. .rx_lvl_offset = 15,
  1263. .tx_st_done = 25,
  1264. .high_speed = true,
  1265. };
  1266. struct s3c64xx_spi_port_config exynos4_spi_port_config = {
  1267. .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
  1268. .rx_lvl_offset = 15,
  1269. .tx_st_done = 25,
  1270. .high_speed = true,
  1271. .clk_from_cmu = true,
  1272. };
  1273. static struct platform_device_id s3c64xx_spi_driver_ids[] = {
  1274. {
  1275. .name = "s3c2443-spi",
  1276. .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
  1277. }, {
  1278. .name = "s3c6410-spi",
  1279. .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
  1280. }, {
  1281. .name = "s5p64x0-spi",
  1282. .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
  1283. }, {
  1284. .name = "s5pc100-spi",
  1285. .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
  1286. }, {
  1287. .name = "s5pv210-spi",
  1288. .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
  1289. }, {
  1290. .name = "exynos4210-spi",
  1291. .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
  1292. },
  1293. { },
  1294. };
  1295. #ifdef CONFIG_OF
  1296. static const struct of_device_id s3c64xx_spi_dt_match[] = {
  1297. { .compatible = "samsung,exynos4210-spi",
  1298. .data = (void *)&exynos4_spi_port_config,
  1299. },
  1300. { },
  1301. };
  1302. MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
  1303. #endif /* CONFIG_OF */
  1304. static struct platform_driver s3c64xx_spi_driver = {
  1305. .driver = {
  1306. .name = "s3c64xx-spi",
  1307. .owner = THIS_MODULE,
  1308. .pm = &s3c64xx_spi_pm,
  1309. .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
  1310. },
  1311. .remove = s3c64xx_spi_remove,
  1312. .id_table = s3c64xx_spi_driver_ids,
  1313. };
  1314. MODULE_ALIAS("platform:s3c64xx-spi");
  1315. static int __init s3c64xx_spi_init(void)
  1316. {
  1317. return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
  1318. }
  1319. subsys_initcall(s3c64xx_spi_init);
  1320. static void __exit s3c64xx_spi_exit(void)
  1321. {
  1322. platform_driver_unregister(&s3c64xx_spi_driver);
  1323. }
  1324. module_exit(s3c64xx_spi_exit);
  1325. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  1326. MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
  1327. MODULE_LICENSE("GPL");