spi-s3c64xx.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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_SW_RST;
  192. val &= ~S3C64XX_SPI_CH_HS_EN;
  193. writel(val, regs + S3C64XX_SPI_CH_CFG);
  194. /* Flush TxFIFO*/
  195. loops = msecs_to_loops(1);
  196. do {
  197. val = readl(regs + S3C64XX_SPI_STATUS);
  198. } while (TX_FIFO_LVL(val, sdd) && loops--);
  199. if (loops == 0)
  200. dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
  201. /* Flush RxFIFO*/
  202. loops = msecs_to_loops(1);
  203. do {
  204. val = readl(regs + S3C64XX_SPI_STATUS);
  205. if (RX_FIFO_LVL(val, sdd))
  206. readl(regs + S3C64XX_SPI_RX_DATA);
  207. else
  208. break;
  209. } while (loops--);
  210. if (loops == 0)
  211. dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
  212. val = readl(regs + S3C64XX_SPI_CH_CFG);
  213. val &= ~S3C64XX_SPI_CH_SW_RST;
  214. writel(val, regs + S3C64XX_SPI_CH_CFG);
  215. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  216. val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
  217. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  218. val = readl(regs + S3C64XX_SPI_CH_CFG);
  219. val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
  220. writel(val, regs + S3C64XX_SPI_CH_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(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_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. else
  653. disable_cs(sdd, spi);
  654. }
  655. msg->actual_length += xfer->len;
  656. flush_fifo(sdd);
  657. }
  658. out:
  659. if (!cs_toggle || status)
  660. disable_cs(sdd, spi);
  661. else
  662. sdd->tgl_spi = spi;
  663. s3c64xx_spi_unmap_mssg(sdd, msg);
  664. msg->status = status;
  665. spi_finalize_current_message(master);
  666. return 0;
  667. }
  668. static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
  669. {
  670. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  671. /* Acquire DMA channels */
  672. while (!acquire_dma(sdd))
  673. msleep(10);
  674. pm_runtime_get_sync(&sdd->pdev->dev);
  675. return 0;
  676. }
  677. static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
  678. {
  679. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
  680. /* Free DMA channels */
  681. sdd->ops->release(sdd->rx_dma.ch, &s3c64xx_spi_dma_client);
  682. sdd->ops->release(sdd->tx_dma.ch, &s3c64xx_spi_dma_client);
  683. pm_runtime_put(&sdd->pdev->dev);
  684. return 0;
  685. }
  686. static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
  687. struct s3c64xx_spi_driver_data *sdd,
  688. struct spi_device *spi)
  689. {
  690. struct s3c64xx_spi_csinfo *cs;
  691. struct device_node *slave_np, *data_np = NULL;
  692. u32 fb_delay = 0;
  693. slave_np = spi->dev.of_node;
  694. if (!slave_np) {
  695. dev_err(&spi->dev, "device node not found\n");
  696. return ERR_PTR(-EINVAL);
  697. }
  698. data_np = of_get_child_by_name(slave_np, "controller-data");
  699. if (!data_np) {
  700. dev_err(&spi->dev, "child node 'controller-data' not found\n");
  701. return ERR_PTR(-EINVAL);
  702. }
  703. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  704. if (!cs) {
  705. dev_err(&spi->dev, "could not allocate memory for controller"
  706. " data\n");
  707. of_node_put(data_np);
  708. return ERR_PTR(-ENOMEM);
  709. }
  710. cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
  711. if (!gpio_is_valid(cs->line)) {
  712. dev_err(&spi->dev, "chip select gpio is not specified or "
  713. "invalid\n");
  714. kfree(cs);
  715. of_node_put(data_np);
  716. return ERR_PTR(-EINVAL);
  717. }
  718. of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
  719. cs->fb_delay = fb_delay;
  720. of_node_put(data_np);
  721. return cs;
  722. }
  723. /*
  724. * Here we only check the validity of requested configuration
  725. * and save the configuration in a local data-structure.
  726. * The controller is actually configured only just before we
  727. * get a message to transfer.
  728. */
  729. static int s3c64xx_spi_setup(struct spi_device *spi)
  730. {
  731. struct s3c64xx_spi_csinfo *cs = spi->controller_data;
  732. struct s3c64xx_spi_driver_data *sdd;
  733. struct s3c64xx_spi_info *sci;
  734. struct spi_message *msg;
  735. unsigned long flags;
  736. int err;
  737. sdd = spi_master_get_devdata(spi->master);
  738. if (!cs && spi->dev.of_node) {
  739. cs = s3c64xx_get_slave_ctrldata(sdd, spi);
  740. spi->controller_data = cs;
  741. }
  742. if (IS_ERR_OR_NULL(cs)) {
  743. dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
  744. return -ENODEV;
  745. }
  746. if (!spi_get_ctldata(spi)) {
  747. err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
  748. dev_name(&spi->dev));
  749. if (err) {
  750. dev_err(&spi->dev,
  751. "Failed to get /CS gpio [%d]: %d\n",
  752. cs->line, err);
  753. goto err_gpio_req;
  754. }
  755. spi_set_ctldata(spi, cs);
  756. }
  757. sci = sdd->cntrlr_info;
  758. spin_lock_irqsave(&sdd->lock, flags);
  759. list_for_each_entry(msg, &sdd->queue, queue) {
  760. /* Is some mssg is already queued for this device */
  761. if (msg->spi == spi) {
  762. dev_err(&spi->dev,
  763. "setup: attempt while mssg in queue!\n");
  764. spin_unlock_irqrestore(&sdd->lock, flags);
  765. err = -EBUSY;
  766. goto err_msgq;
  767. }
  768. }
  769. spin_unlock_irqrestore(&sdd->lock, flags);
  770. if (spi->bits_per_word != 8
  771. && spi->bits_per_word != 16
  772. && spi->bits_per_word != 32) {
  773. dev_err(&spi->dev, "setup: %dbits/wrd not supported!\n",
  774. spi->bits_per_word);
  775. err = -EINVAL;
  776. goto setup_exit;
  777. }
  778. pm_runtime_get_sync(&sdd->pdev->dev);
  779. /* Check if we can provide the requested rate */
  780. if (!sdd->port_conf->clk_from_cmu) {
  781. u32 psr, speed;
  782. /* Max possible */
  783. speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
  784. if (spi->max_speed_hz > speed)
  785. spi->max_speed_hz = speed;
  786. psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
  787. psr &= S3C64XX_SPI_PSR_MASK;
  788. if (psr == S3C64XX_SPI_PSR_MASK)
  789. psr--;
  790. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  791. if (spi->max_speed_hz < speed) {
  792. if (psr+1 < S3C64XX_SPI_PSR_MASK) {
  793. psr++;
  794. } else {
  795. err = -EINVAL;
  796. goto setup_exit;
  797. }
  798. }
  799. speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
  800. if (spi->max_speed_hz >= speed) {
  801. spi->max_speed_hz = speed;
  802. } else {
  803. err = -EINVAL;
  804. goto setup_exit;
  805. }
  806. }
  807. pm_runtime_put(&sdd->pdev->dev);
  808. disable_cs(sdd, spi);
  809. return 0;
  810. setup_exit:
  811. /* setup() returns with device de-selected */
  812. disable_cs(sdd, spi);
  813. err_msgq:
  814. gpio_free(cs->line);
  815. spi_set_ctldata(spi, NULL);
  816. err_gpio_req:
  817. if (spi->dev.of_node)
  818. kfree(cs);
  819. return err;
  820. }
  821. static void s3c64xx_spi_cleanup(struct spi_device *spi)
  822. {
  823. struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
  824. if (cs) {
  825. gpio_free(cs->line);
  826. if (spi->dev.of_node)
  827. kfree(cs);
  828. }
  829. spi_set_ctldata(spi, NULL);
  830. }
  831. static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
  832. {
  833. struct s3c64xx_spi_driver_data *sdd = data;
  834. struct spi_master *spi = sdd->master;
  835. unsigned int val;
  836. val = readl(sdd->regs + S3C64XX_SPI_PENDING_CLR);
  837. val &= S3C64XX_SPI_PND_RX_OVERRUN_CLR |
  838. S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
  839. S3C64XX_SPI_PND_TX_OVERRUN_CLR |
  840. S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
  841. writel(val, sdd->regs + S3C64XX_SPI_PENDING_CLR);
  842. if (val & S3C64XX_SPI_PND_RX_OVERRUN_CLR)
  843. dev_err(&spi->dev, "RX overrun\n");
  844. if (val & S3C64XX_SPI_PND_RX_UNDERRUN_CLR)
  845. dev_err(&spi->dev, "RX underrun\n");
  846. if (val & S3C64XX_SPI_PND_TX_OVERRUN_CLR)
  847. dev_err(&spi->dev, "TX overrun\n");
  848. if (val & S3C64XX_SPI_PND_TX_UNDERRUN_CLR)
  849. dev_err(&spi->dev, "TX underrun\n");
  850. return IRQ_HANDLED;
  851. }
  852. static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
  853. {
  854. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  855. void __iomem *regs = sdd->regs;
  856. unsigned int val;
  857. sdd->cur_speed = 0;
  858. writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
  859. /* Disable Interrupts - we use Polling if not DMA mode */
  860. writel(0, regs + S3C64XX_SPI_INT_EN);
  861. if (!sdd->port_conf->clk_from_cmu)
  862. writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
  863. regs + S3C64XX_SPI_CLK_CFG);
  864. writel(0, regs + S3C64XX_SPI_MODE_CFG);
  865. writel(0, regs + S3C64XX_SPI_PACKET_CNT);
  866. /* Clear any irq pending bits */
  867. writel(readl(regs + S3C64XX_SPI_PENDING_CLR),
  868. regs + S3C64XX_SPI_PENDING_CLR);
  869. writel(0, regs + S3C64XX_SPI_SWAP_CFG);
  870. val = readl(regs + S3C64XX_SPI_MODE_CFG);
  871. val &= ~S3C64XX_SPI_MODE_4BURST;
  872. val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  873. val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
  874. writel(val, regs + S3C64XX_SPI_MODE_CFG);
  875. flush_fifo(sdd);
  876. }
  877. static int __devinit s3c64xx_spi_get_dmares(
  878. struct s3c64xx_spi_driver_data *sdd, bool tx)
  879. {
  880. struct platform_device *pdev = sdd->pdev;
  881. struct s3c64xx_spi_dma_data *dma_data;
  882. struct property *prop;
  883. struct resource *res;
  884. char prop_name[15], *chan_str;
  885. if (tx) {
  886. dma_data = &sdd->tx_dma;
  887. dma_data->direction = DMA_MEM_TO_DEV;
  888. chan_str = "tx";
  889. } else {
  890. dma_data = &sdd->rx_dma;
  891. dma_data->direction = DMA_DEV_TO_MEM;
  892. chan_str = "rx";
  893. }
  894. if (!sdd->pdev->dev.of_node) {
  895. res = platform_get_resource(pdev, IORESOURCE_DMA, tx ? 0 : 1);
  896. if (!res) {
  897. dev_err(&pdev->dev, "Unable to get SPI-%s dma "
  898. "resource\n", chan_str);
  899. return -ENXIO;
  900. }
  901. dma_data->dmach = res->start;
  902. return 0;
  903. }
  904. sprintf(prop_name, "%s-dma-channel", chan_str);
  905. prop = of_find_property(pdev->dev.of_node, prop_name, NULL);
  906. if (!prop) {
  907. dev_err(&pdev->dev, "%s dma channel property not specified\n",
  908. chan_str);
  909. return -ENXIO;
  910. }
  911. dma_data->dmach = DMACH_DT_PROP;
  912. dma_data->dma_prop = prop;
  913. return 0;
  914. }
  915. #ifdef CONFIG_OF
  916. static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
  917. {
  918. struct device *dev = &sdd->pdev->dev;
  919. int idx, gpio, ret;
  920. /* find gpios for mosi, miso and clock lines */
  921. for (idx = 0; idx < 3; idx++) {
  922. gpio = of_get_gpio(dev->of_node, idx);
  923. if (!gpio_is_valid(gpio)) {
  924. dev_err(dev, "invalid gpio[%d]: %d\n", idx, gpio);
  925. goto free_gpio;
  926. }
  927. ret = gpio_request(gpio, "spi-bus");
  928. if (ret) {
  929. dev_err(dev, "gpio [%d] request failed: %d\n",
  930. gpio, ret);
  931. goto free_gpio;
  932. }
  933. }
  934. return 0;
  935. free_gpio:
  936. while (--idx >= 0)
  937. gpio_free(sdd->gpios[idx]);
  938. return -EINVAL;
  939. }
  940. static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
  941. {
  942. unsigned int idx;
  943. for (idx = 0; idx < 3; idx++)
  944. gpio_free(sdd->gpios[idx]);
  945. }
  946. static struct __devinit s3c64xx_spi_info * s3c64xx_spi_parse_dt(
  947. struct device *dev)
  948. {
  949. struct s3c64xx_spi_info *sci;
  950. u32 temp;
  951. sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
  952. if (!sci) {
  953. dev_err(dev, "memory allocation for spi_info failed\n");
  954. return ERR_PTR(-ENOMEM);
  955. }
  956. if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
  957. dev_warn(dev, "spi bus clock parent not specified, using "
  958. "clock at index 0 as parent\n");
  959. sci->src_clk_nr = 0;
  960. } else {
  961. sci->src_clk_nr = temp;
  962. }
  963. if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
  964. dev_warn(dev, "number of chip select lines not specified, "
  965. "assuming 1 chip select line\n");
  966. sci->num_cs = 1;
  967. } else {
  968. sci->num_cs = temp;
  969. }
  970. return sci;
  971. }
  972. #else
  973. static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
  974. {
  975. return dev->platform_data;
  976. }
  977. static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
  978. {
  979. return -EINVAL;
  980. }
  981. static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
  982. {
  983. }
  984. #endif
  985. static const struct of_device_id s3c64xx_spi_dt_match[];
  986. static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
  987. struct platform_device *pdev)
  988. {
  989. #ifdef CONFIG_OF
  990. if (pdev->dev.of_node) {
  991. const struct of_device_id *match;
  992. match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
  993. return (struct s3c64xx_spi_port_config *)match->data;
  994. }
  995. #endif
  996. return (struct s3c64xx_spi_port_config *)
  997. platform_get_device_id(pdev)->driver_data;
  998. }
  999. static int __init s3c64xx_spi_probe(struct platform_device *pdev)
  1000. {
  1001. struct resource *mem_res;
  1002. struct s3c64xx_spi_driver_data *sdd;
  1003. struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
  1004. struct spi_master *master;
  1005. int ret, irq;
  1006. char clk_name[16];
  1007. if (!sci && pdev->dev.of_node) {
  1008. sci = s3c64xx_spi_parse_dt(&pdev->dev);
  1009. if (IS_ERR(sci))
  1010. return PTR_ERR(sci);
  1011. }
  1012. if (!sci) {
  1013. dev_err(&pdev->dev, "platform_data missing!\n");
  1014. return -ENODEV;
  1015. }
  1016. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1017. if (mem_res == NULL) {
  1018. dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
  1019. return -ENXIO;
  1020. }
  1021. irq = platform_get_irq(pdev, 0);
  1022. if (irq < 0) {
  1023. dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  1024. return irq;
  1025. }
  1026. master = spi_alloc_master(&pdev->dev,
  1027. sizeof(struct s3c64xx_spi_driver_data));
  1028. if (master == NULL) {
  1029. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  1030. return -ENOMEM;
  1031. }
  1032. platform_set_drvdata(pdev, master);
  1033. sdd = spi_master_get_devdata(master);
  1034. sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
  1035. sdd->master = master;
  1036. sdd->cntrlr_info = sci;
  1037. sdd->pdev = pdev;
  1038. sdd->sfr_start = mem_res->start;
  1039. if (pdev->dev.of_node) {
  1040. ret = of_alias_get_id(pdev->dev.of_node, "spi");
  1041. if (ret < 0) {
  1042. dev_err(&pdev->dev, "failed to get alias id, "
  1043. "errno %d\n", ret);
  1044. goto err0;
  1045. }
  1046. sdd->port_id = ret;
  1047. } else {
  1048. sdd->port_id = pdev->id;
  1049. }
  1050. sdd->cur_bpw = 8;
  1051. ret = s3c64xx_spi_get_dmares(sdd, true);
  1052. if (ret)
  1053. goto err0;
  1054. ret = s3c64xx_spi_get_dmares(sdd, false);
  1055. if (ret)
  1056. goto err0;
  1057. master->dev.of_node = pdev->dev.of_node;
  1058. master->bus_num = sdd->port_id;
  1059. master->setup = s3c64xx_spi_setup;
  1060. master->cleanup = s3c64xx_spi_cleanup;
  1061. master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
  1062. master->transfer_one_message = s3c64xx_spi_transfer_one_message;
  1063. master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
  1064. master->num_chipselect = sci->num_cs;
  1065. master->dma_alignment = 8;
  1066. /* the spi->mode bits understood by this driver: */
  1067. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1068. sdd->regs = devm_request_and_ioremap(&pdev->dev, mem_res);
  1069. if (sdd->regs == NULL) {
  1070. dev_err(&pdev->dev, "Unable to remap IO\n");
  1071. ret = -ENXIO;
  1072. goto err1;
  1073. }
  1074. if (!sci->cfg_gpio && pdev->dev.of_node) {
  1075. if (s3c64xx_spi_parse_dt_gpio(sdd))
  1076. return -EBUSY;
  1077. } else if (sci->cfg_gpio == NULL || sci->cfg_gpio()) {
  1078. dev_err(&pdev->dev, "Unable to config gpio\n");
  1079. ret = -EBUSY;
  1080. goto err2;
  1081. }
  1082. /* Setup clocks */
  1083. sdd->clk = clk_get(&pdev->dev, "spi");
  1084. if (IS_ERR(sdd->clk)) {
  1085. dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
  1086. ret = PTR_ERR(sdd->clk);
  1087. goto err3;
  1088. }
  1089. if (clk_enable(sdd->clk)) {
  1090. dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
  1091. ret = -EBUSY;
  1092. goto err4;
  1093. }
  1094. sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
  1095. sdd->src_clk = clk_get(&pdev->dev, clk_name);
  1096. if (IS_ERR(sdd->src_clk)) {
  1097. dev_err(&pdev->dev,
  1098. "Unable to acquire clock '%s'\n", clk_name);
  1099. ret = PTR_ERR(sdd->src_clk);
  1100. goto err5;
  1101. }
  1102. if (clk_enable(sdd->src_clk)) {
  1103. dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
  1104. ret = -EBUSY;
  1105. goto err6;
  1106. }
  1107. /* Setup Deufult Mode */
  1108. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1109. spin_lock_init(&sdd->lock);
  1110. init_completion(&sdd->xfer_completion);
  1111. INIT_LIST_HEAD(&sdd->queue);
  1112. ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd);
  1113. if (ret != 0) {
  1114. dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
  1115. irq, ret);
  1116. goto err7;
  1117. }
  1118. writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
  1119. S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
  1120. sdd->regs + S3C64XX_SPI_INT_EN);
  1121. if (spi_register_master(master)) {
  1122. dev_err(&pdev->dev, "cannot register SPI master\n");
  1123. ret = -EBUSY;
  1124. goto err8;
  1125. }
  1126. dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
  1127. "with %d Slaves attached\n",
  1128. sdd->port_id, master->num_chipselect);
  1129. dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
  1130. mem_res->end, mem_res->start,
  1131. sdd->rx_dma.dmach, sdd->tx_dma.dmach);
  1132. pm_runtime_enable(&pdev->dev);
  1133. return 0;
  1134. err8:
  1135. free_irq(irq, sdd);
  1136. err7:
  1137. clk_disable(sdd->src_clk);
  1138. err6:
  1139. clk_put(sdd->src_clk);
  1140. err5:
  1141. clk_disable(sdd->clk);
  1142. err4:
  1143. clk_put(sdd->clk);
  1144. err3:
  1145. if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
  1146. s3c64xx_spi_dt_gpio_free(sdd);
  1147. err2:
  1148. err1:
  1149. err0:
  1150. platform_set_drvdata(pdev, NULL);
  1151. spi_master_put(master);
  1152. return ret;
  1153. }
  1154. static int s3c64xx_spi_remove(struct platform_device *pdev)
  1155. {
  1156. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  1157. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1158. pm_runtime_disable(&pdev->dev);
  1159. spi_unregister_master(master);
  1160. writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
  1161. free_irq(platform_get_irq(pdev, 0), sdd);
  1162. clk_disable(sdd->src_clk);
  1163. clk_put(sdd->src_clk);
  1164. clk_disable(sdd->clk);
  1165. clk_put(sdd->clk);
  1166. if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
  1167. s3c64xx_spi_dt_gpio_free(sdd);
  1168. platform_set_drvdata(pdev, NULL);
  1169. spi_master_put(master);
  1170. return 0;
  1171. }
  1172. #ifdef CONFIG_PM
  1173. static int s3c64xx_spi_suspend(struct device *dev)
  1174. {
  1175. struct spi_master *master = dev_get_drvdata(dev);
  1176. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1177. spi_master_suspend(master);
  1178. /* Disable the clock */
  1179. clk_disable(sdd->src_clk);
  1180. clk_disable(sdd->clk);
  1181. if (!sdd->cntrlr_info->cfg_gpio && dev->of_node)
  1182. s3c64xx_spi_dt_gpio_free(sdd);
  1183. sdd->cur_speed = 0; /* Output Clock is stopped */
  1184. return 0;
  1185. }
  1186. static int s3c64xx_spi_resume(struct device *dev)
  1187. {
  1188. struct spi_master *master = dev_get_drvdata(dev);
  1189. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1190. struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
  1191. if (!sci->cfg_gpio && dev->of_node)
  1192. s3c64xx_spi_parse_dt_gpio(sdd);
  1193. else
  1194. sci->cfg_gpio();
  1195. /* Enable the clock */
  1196. clk_enable(sdd->src_clk);
  1197. clk_enable(sdd->clk);
  1198. s3c64xx_spi_hwinit(sdd, sdd->port_id);
  1199. spi_master_resume(master);
  1200. return 0;
  1201. }
  1202. #endif /* CONFIG_PM */
  1203. #ifdef CONFIG_PM_RUNTIME
  1204. static int s3c64xx_spi_runtime_suspend(struct device *dev)
  1205. {
  1206. struct spi_master *master = dev_get_drvdata(dev);
  1207. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1208. clk_disable(sdd->clk);
  1209. clk_disable(sdd->src_clk);
  1210. return 0;
  1211. }
  1212. static int s3c64xx_spi_runtime_resume(struct device *dev)
  1213. {
  1214. struct spi_master *master = dev_get_drvdata(dev);
  1215. struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
  1216. clk_enable(sdd->src_clk);
  1217. clk_enable(sdd->clk);
  1218. return 0;
  1219. }
  1220. #endif /* CONFIG_PM_RUNTIME */
  1221. static const struct dev_pm_ops s3c64xx_spi_pm = {
  1222. SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
  1223. SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
  1224. s3c64xx_spi_runtime_resume, NULL)
  1225. };
  1226. static struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
  1227. .fifo_lvl_mask = { 0x7f },
  1228. .rx_lvl_offset = 13,
  1229. .tx_st_done = 21,
  1230. .high_speed = true,
  1231. };
  1232. static struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
  1233. .fifo_lvl_mask = { 0x7f, 0x7F },
  1234. .rx_lvl_offset = 13,
  1235. .tx_st_done = 21,
  1236. };
  1237. static struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
  1238. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1239. .rx_lvl_offset = 15,
  1240. .tx_st_done = 25,
  1241. };
  1242. static struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
  1243. .fifo_lvl_mask = { 0x7f, 0x7F },
  1244. .rx_lvl_offset = 13,
  1245. .tx_st_done = 21,
  1246. .high_speed = true,
  1247. };
  1248. static struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
  1249. .fifo_lvl_mask = { 0x1ff, 0x7F },
  1250. .rx_lvl_offset = 15,
  1251. .tx_st_done = 25,
  1252. .high_speed = true,
  1253. };
  1254. static struct s3c64xx_spi_port_config exynos4_spi_port_config = {
  1255. .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
  1256. .rx_lvl_offset = 15,
  1257. .tx_st_done = 25,
  1258. .high_speed = true,
  1259. .clk_from_cmu = true,
  1260. };
  1261. static struct platform_device_id s3c64xx_spi_driver_ids[] = {
  1262. {
  1263. .name = "s3c2443-spi",
  1264. .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
  1265. }, {
  1266. .name = "s3c6410-spi",
  1267. .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
  1268. }, {
  1269. .name = "s5p64x0-spi",
  1270. .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
  1271. }, {
  1272. .name = "s5pc100-spi",
  1273. .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
  1274. }, {
  1275. .name = "s5pv210-spi",
  1276. .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
  1277. }, {
  1278. .name = "exynos4210-spi",
  1279. .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
  1280. },
  1281. { },
  1282. };
  1283. #ifdef CONFIG_OF
  1284. static const struct of_device_id s3c64xx_spi_dt_match[] = {
  1285. { .compatible = "samsung,exynos4210-spi",
  1286. .data = (void *)&exynos4_spi_port_config,
  1287. },
  1288. { },
  1289. };
  1290. MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
  1291. #endif /* CONFIG_OF */
  1292. static struct platform_driver s3c64xx_spi_driver = {
  1293. .driver = {
  1294. .name = "s3c64xx-spi",
  1295. .owner = THIS_MODULE,
  1296. .pm = &s3c64xx_spi_pm,
  1297. .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
  1298. },
  1299. .remove = s3c64xx_spi_remove,
  1300. .id_table = s3c64xx_spi_driver_ids,
  1301. };
  1302. MODULE_ALIAS("platform:s3c64xx-spi");
  1303. static int __init s3c64xx_spi_init(void)
  1304. {
  1305. return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
  1306. }
  1307. subsys_initcall(s3c64xx_spi_init);
  1308. static void __exit s3c64xx_spi_exit(void)
  1309. {
  1310. platform_driver_unregister(&s3c64xx_spi_driver);
  1311. }
  1312. module_exit(s3c64xx_spi_exit);
  1313. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  1314. MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
  1315. MODULE_LICENSE("GPL");