spi-s3c64xx.c 40 KB

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