spi-s3c64xx.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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, "request for slave select gpio "
  751. "line [%d] failed\n", cs->line);
  752. err = -EBUSY;
  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\n", gpio);
  929. goto free_gpio;
  930. }
  931. }
  932. return 0;
  933. free_gpio:
  934. while (--idx >= 0)
  935. gpio_free(sdd->gpios[idx]);
  936. return -EINVAL;
  937. }
  938. static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
  939. {
  940. unsigned int idx;
  941. for (idx = 0; idx < 3; idx++)
  942. gpio_free(sdd->gpios[idx]);
  943. }
  944. static struct __devinit s3c64xx_spi_info * s3c64xx_spi_parse_dt(
  945. struct device *dev)
  946. {
  947. struct s3c64xx_spi_info *sci;
  948. u32 temp;
  949. sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
  950. if (!sci) {
  951. dev_err(dev, "memory allocation for spi_info failed\n");
  952. return ERR_PTR(-ENOMEM);
  953. }
  954. if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
  955. dev_warn(dev, "spi bus clock parent not specified, using "
  956. "clock at index 0 as parent\n");
  957. sci->src_clk_nr = 0;
  958. } else {
  959. sci->src_clk_nr = temp;
  960. }
  961. if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
  962. dev_warn(dev, "number of chip select lines not specified, "
  963. "assuming 1 chip select line\n");
  964. sci->num_cs = 1;
  965. } else {
  966. sci->num_cs = temp;
  967. }
  968. return sci;
  969. }
  970. #else
  971. static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
  972. {
  973. return dev->platform_data;
  974. }
  975. static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
  976. {
  977. return -EINVAL;
  978. }
  979. static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
  980. {
  981. }
  982. #endif
  983. static const struct of_device_id s3c64xx_spi_dt_match[];
  984. static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
  985. struct platform_device *pdev)
  986. {
  987. #ifdef CONFIG_OF
  988. if (pdev->dev.of_node) {
  989. const struct of_device_id *match;
  990. match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
  991. return (struct s3c64xx_spi_port_config *)match->data;
  992. }
  993. #endif
  994. return (struct s3c64xx_spi_port_config *)
  995. platform_get_device_id(pdev)->driver_data;
  996. }
  997. static int __init s3c64xx_spi_probe(struct platform_device *pdev)
  998. {
  999. struct resource *mem_res;
  1000. struct s3c64xx_spi_driver_data *sdd;
  1001. struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
  1002. struct spi_master *master;
  1003. int ret, irq;
  1004. char clk_name[16];
  1005. if (!sci && pdev->dev.of_node) {
  1006. sci = s3c64xx_spi_parse_dt(&pdev->dev);
  1007. if (IS_ERR(sci))
  1008. return PTR_ERR(sci);
  1009. }
  1010. if (!sci) {
  1011. dev_err(&pdev->dev, "platform_data missing!\n");
  1012. return -ENODEV;
  1013. }
  1014. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1015. if (mem_res == NULL) {
  1016. dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
  1017. return -ENXIO;
  1018. }
  1019. irq = platform_get_irq(pdev, 0);
  1020. if (irq < 0) {
  1021. dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  1022. return irq;
  1023. }
  1024. master = spi_alloc_master(&pdev->dev,
  1025. sizeof(struct s3c64xx_spi_driver_data));
  1026. if (master == NULL) {
  1027. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  1028. return -ENOMEM;
  1029. }
  1030. platform_set_drvdata(pdev, master);
  1031. sdd = spi_master_get_devdata(master);
  1032. sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
  1033. sdd->master = master;
  1034. sdd->cntrlr_info = sci;
  1035. sdd->pdev = pdev;
  1036. sdd->sfr_start = mem_res->start;
  1037. if (pdev->dev.of_node) {
  1038. ret = of_alias_get_id(pdev->dev.of_node, "spi");
  1039. if (ret < 0) {
  1040. dev_err(&pdev->dev, "failed to get alias id, "
  1041. "errno %d\n", ret);
  1042. goto err0;
  1043. }
  1044. sdd->port_id = ret;
  1045. } else {
  1046. sdd->port_id = pdev->id;
  1047. }
  1048. sdd->cur_bpw = 8;
  1049. ret = s3c64xx_spi_get_dmares(sdd, true);
  1050. if (ret)
  1051. goto err0;
  1052. ret = s3c64xx_spi_get_dmares(sdd, false);
  1053. if (ret)
  1054. goto err0;
  1055. master->dev.of_node = pdev->dev.of_node;
  1056. master->bus_num = sdd->port_id;
  1057. master->setup = s3c64xx_spi_setup;
  1058. master->cleanup = s3c64xx_spi_cleanup;
  1059. master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
  1060. master->transfer_one_message = s3c64xx_spi_transfer_one_message;
  1061. master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
  1062. master->num_chipselect = sci->num_cs;
  1063. master->dma_alignment = 8;
  1064. /* the spi->mode bits understood by this driver: */
  1065. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1066. if (request_mem_region(mem_res->start,
  1067. resource_size(mem_res), pdev->name) == NULL) {
  1068. dev_err(&pdev->dev, "Req mem region failed\n");
  1069. ret = -ENXIO;
  1070. goto err0;
  1071. }
  1072. sdd->regs = ioremap(mem_res->start, resource_size(mem_res));
  1073. if (sdd->regs == NULL) {
  1074. dev_err(&pdev->dev, "Unable to remap IO\n");
  1075. ret = -ENXIO;
  1076. goto err1;
  1077. }
  1078. if (!sci->cfg_gpio && pdev->dev.of_node) {
  1079. if (s3c64xx_spi_parse_dt_gpio(sdd))
  1080. return -EBUSY;
  1081. } else if (sci->cfg_gpio == NULL || sci->cfg_gpio()) {
  1082. dev_err(&pdev->dev, "Unable to config gpio\n");
  1083. ret = -EBUSY;
  1084. goto err2;
  1085. }
  1086. /* Setup clocks */
  1087. sdd->clk = clk_get(&pdev->dev, "spi");
  1088. if (IS_ERR(sdd->clk)) {
  1089. dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
  1090. ret = PTR_ERR(sdd->clk);
  1091. goto err3;
  1092. }
  1093. if (clk_enable(sdd->clk)) {
  1094. dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
  1095. ret = -EBUSY;
  1096. goto err4;
  1097. }
  1098. sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
  1099. sdd->src_clk = clk_get(&pdev->dev, clk_name);
  1100. if (IS_ERR(sdd->src_clk)) {
  1101. dev_err(&pdev->dev,
  1102. "Unable to acquire clock '%s'\n", clk_name);
  1103. ret = PTR_ERR(sdd->src_clk);
  1104. goto err5;
  1105. }
  1106. if (clk_enable(sdd->src_clk)) {
  1107. dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
  1108. ret = -EBUSY;
  1109. goto err6;
  1110. }
  1111. /* Setup Deufult Mode */
  1112. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1113. spin_lock_init(&sdd->lock);
  1114. init_completion(&sdd->xfer_completion);
  1115. INIT_LIST_HEAD(&sdd->queue);
  1116. ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd);
  1117. if (ret != 0) {
  1118. dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
  1119. irq, ret);
  1120. goto err7;
  1121. }
  1122. writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
  1123. S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
  1124. sdd->regs + S3C64XX_SPI_INT_EN);
  1125. if (spi_register_master(master)) {
  1126. dev_err(&pdev->dev, "cannot register SPI master\n");
  1127. ret = -EBUSY;
  1128. goto err8;
  1129. }
  1130. dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
  1131. "with %d Slaves attached\n",
  1132. sdd->port_id, master->num_chipselect);
  1133. dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
  1134. mem_res->end, mem_res->start,
  1135. sdd->rx_dma.dmach, sdd->tx_dma.dmach);
  1136. pm_runtime_enable(&pdev->dev);
  1137. return 0;
  1138. err8:
  1139. free_irq(irq, sdd);
  1140. err7:
  1141. clk_disable(sdd->src_clk);
  1142. err6:
  1143. clk_put(sdd->src_clk);
  1144. err5:
  1145. clk_disable(sdd->clk);
  1146. err4:
  1147. clk_put(sdd->clk);
  1148. err3:
  1149. if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
  1150. s3c64xx_spi_dt_gpio_free(sdd);
  1151. err2:
  1152. iounmap((void *) sdd->regs);
  1153. err1:
  1154. release_mem_region(mem_res->start, resource_size(mem_res));
  1155. err0:
  1156. platform_set_drvdata(pdev, NULL);
  1157. spi_master_put(master);
  1158. return ret;
  1159. }
  1160. static int s3c64xx_spi_remove(struct platform_device *pdev)
  1161. {
  1162. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  1163. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1164. struct resource *mem_res;
  1165. pm_runtime_disable(&pdev->dev);
  1166. spi_unregister_master(master);
  1167. writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
  1168. free_irq(platform_get_irq(pdev, 0), sdd);
  1169. clk_disable(sdd->src_clk);
  1170. clk_put(sdd->src_clk);
  1171. clk_disable(sdd->clk);
  1172. clk_put(sdd->clk);
  1173. if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
  1174. s3c64xx_spi_dt_gpio_free(sdd);
  1175. iounmap((void *) sdd->regs);
  1176. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1177. if (mem_res != NULL)
  1178. release_mem_region(mem_res->start, resource_size(mem_res));
  1179. platform_set_drvdata(pdev, NULL);
  1180. spi_master_put(master);
  1181. return 0;
  1182. }
  1183. #ifdef CONFIG_PM
  1184. static int s3c64xx_spi_suspend(struct device *dev)
  1185. {
  1186. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1187. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1188. spi_master_suspend(master);
  1189. /* Disable the clock */
  1190. clk_disable(sdd->src_clk);
  1191. clk_disable(sdd->clk);
  1192. if (!sdd->cntrlr_info->cfg_gpio && dev->of_node)
  1193. s3c64xx_spi_dt_gpio_free(sdd);
  1194. sdd->cur_speed = 0; /* Output Clock is stopped */
  1195. return 0;
  1196. }
  1197. static int s3c64xx_spi_resume(struct device *dev)
  1198. {
  1199. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1200. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1201. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  1202. if (!sci->cfg_gpio && dev->of_node)
  1203. s3c64xx_spi_parse_dt_gpio(sdd);
  1204. else
  1205. sci->cfg_gpio();
  1206. /* Enable the clock */
  1207. clk_enable(sdd->src_clk);
  1208. clk_enable(sdd->clk);
  1209. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1210. spi_master_resume(master);
  1211. return 0;
  1212. }
  1213. #endif /* CONFIG_PM */
  1214. #ifdef CONFIG_PM_RUNTIME
  1215. static int s3c64xx_spi_runtime_suspend(struct device *dev)
  1216. {
  1217. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1218. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1219. clk_disable(sdd->clk);
  1220. clk_disable(sdd->src_clk);
  1221. return 0;
  1222. }
  1223. static int s3c64xx_spi_runtime_resume(struct device *dev)
  1224. {
  1225. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  1226. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1227. clk_enable(sdd->src_clk);
  1228. clk_enable(sdd->clk);
  1229. return 0;
  1230. }
  1231. #endif /* CONFIG_PM_RUNTIME */
  1232. static const struct dev_pm_ops s3c64xx_spi_pm = {
  1233. SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
  1234. SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
  1235. s3c64xx_spi_runtime_resume, NULL)
  1236. };
  1237. struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
  1238. .fifo_lvl_mask = { 0x7f },
  1239. .rx_lvl_offset = 13,
  1240. .tx_st_done = 21,
  1241. .high_speed = true,
  1242. };
  1243. struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
  1244. .fifo_lvl_mask = { 0x7f, 0x7F },
  1245. .rx_lvl_offset = 13,
  1246. .tx_st_done = 21,
  1247. };
  1248. struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
  1249. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1250. .rx_lvl_offset = 15,
  1251. .tx_st_done = 25,
  1252. };
  1253. struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
  1254. .fifo_lvl_mask = { 0x7f, 0x7F },
  1255. .rx_lvl_offset = 13,
  1256. .tx_st_done = 21,
  1257. .high_speed = true,
  1258. };
  1259. struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
  1260. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1261. .rx_lvl_offset = 15,
  1262. .tx_st_done = 25,
  1263. .high_speed = true,
  1264. };
  1265. struct s3c64xx_spi_port_config exynos4_spi_port_config = {
  1266. .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
  1267. .rx_lvl_offset = 15,
  1268. .tx_st_done = 25,
  1269. .high_speed = true,
  1270. .clk_from_cmu = true,
  1271. };
  1272. static struct platform_device_id s3c64xx_spi_driver_ids[] = {
  1273. {
  1274. .name = "s3c2443-spi",
  1275. .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
  1276. }, {
  1277. .name = "s3c6410-spi",
  1278. .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
  1279. }, {
  1280. .name = "s5p64x0-spi",
  1281. .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
  1282. }, {
  1283. .name = "s5pc100-spi",
  1284. .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
  1285. }, {
  1286. .name = "s5pv210-spi",
  1287. .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
  1288. }, {
  1289. .name = "exynos4210-spi",
  1290. .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
  1291. },
  1292. { },
  1293. };
  1294. #ifdef CONFIG_OF
  1295. static const struct of_device_id s3c64xx_spi_dt_match[] = {
  1296. { .compatible = "samsung,exynos4210-spi",
  1297. .data = (void *)&exynos4_spi_port_config,
  1298. },
  1299. { },
  1300. };
  1301. MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
  1302. #endif /* CONFIG_OF */
  1303. static struct platform_driver s3c64xx_spi_driver = {
  1304. .driver = {
  1305. .name = "s3c64xx-spi",
  1306. .owner = THIS_MODULE,
  1307. .pm = &s3c64xx_spi_pm,
  1308. .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
  1309. },
  1310. .remove = s3c64xx_spi_remove,
  1311. .id_table = s3c64xx_spi_driver_ids,
  1312. };
  1313. MODULE_ALIAS("platform:s3c64xx-spi");
  1314. static int __init s3c64xx_spi_init(void)
  1315. {
  1316. return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
  1317. }
  1318. subsys_initcall(s3c64xx_spi_init);
  1319. static void __exit s3c64xx_spi_exit(void)
  1320. {
  1321. platform_driver_unregister(&s3c64xx_spi_driver);
  1322. }
  1323. module_exit(s3c64xx_spi_exit);
  1324. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  1325. MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
  1326. MODULE_LICENSE("GPL");