spi-s3c64xx.c 40 KB

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