spi-tegra20-slink.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*
  2. * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kthread.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/spi/spi-tegra.h>
  37. #include <linux/clk/tegra.h>
  38. #define SLINK_COMMAND 0x000
  39. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  40. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  41. #define SLINK_BOTH_EN (1 << 10)
  42. #define SLINK_CS_SW (1 << 11)
  43. #define SLINK_CS_VALUE (1 << 12)
  44. #define SLINK_CS_POLARITY (1 << 13)
  45. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  46. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  47. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  48. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  49. #define SLINK_IDLE_SDA_MASK (3 << 16)
  50. #define SLINK_CS_POLARITY1 (1 << 20)
  51. #define SLINK_CK_SDA (1 << 21)
  52. #define SLINK_CS_POLARITY2 (1 << 22)
  53. #define SLINK_CS_POLARITY3 (1 << 23)
  54. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  55. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  56. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  57. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  58. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  59. #define SLINK_M_S (1 << 28)
  60. #define SLINK_WAIT (1 << 29)
  61. #define SLINK_GO (1 << 30)
  62. #define SLINK_ENB (1 << 31)
  63. #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  64. #define SLINK_COMMAND2 0x004
  65. #define SLINK_LSBFE (1 << 0)
  66. #define SLINK_SSOE (1 << 1)
  67. #define SLINK_SPIE (1 << 4)
  68. #define SLINK_BIDIROE (1 << 6)
  69. #define SLINK_MODFEN (1 << 7)
  70. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  71. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  72. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  73. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  74. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  75. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  76. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  77. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  78. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  79. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  80. #define SLINK_SPC0 (1 << 29)
  81. #define SLINK_TXEN (1 << 30)
  82. #define SLINK_RXEN (1 << 31)
  83. #define SLINK_STATUS 0x008
  84. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  85. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  86. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  87. #define SLINK_MODF (1 << 16)
  88. #define SLINK_RX_UNF (1 << 18)
  89. #define SLINK_TX_OVF (1 << 19)
  90. #define SLINK_TX_FULL (1 << 20)
  91. #define SLINK_TX_EMPTY (1 << 21)
  92. #define SLINK_RX_FULL (1 << 22)
  93. #define SLINK_RX_EMPTY (1 << 23)
  94. #define SLINK_TX_UNF (1 << 24)
  95. #define SLINK_RX_OVF (1 << 25)
  96. #define SLINK_TX_FLUSH (1 << 26)
  97. #define SLINK_RX_FLUSH (1 << 27)
  98. #define SLINK_SCLK (1 << 28)
  99. #define SLINK_ERR (1 << 29)
  100. #define SLINK_RDY (1 << 30)
  101. #define SLINK_BSY (1 << 31)
  102. #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
  103. SLINK_TX_UNF | SLINK_RX_OVF)
  104. #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  105. #define SLINK_MAS_DATA 0x010
  106. #define SLINK_SLAVE_DATA 0x014
  107. #define SLINK_DMA_CTL 0x018
  108. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  109. #define SLINK_TX_TRIG_1 (0 << 16)
  110. #define SLINK_TX_TRIG_4 (1 << 16)
  111. #define SLINK_TX_TRIG_8 (2 << 16)
  112. #define SLINK_TX_TRIG_16 (3 << 16)
  113. #define SLINK_TX_TRIG_MASK (3 << 16)
  114. #define SLINK_RX_TRIG_1 (0 << 18)
  115. #define SLINK_RX_TRIG_4 (1 << 18)
  116. #define SLINK_RX_TRIG_8 (2 << 18)
  117. #define SLINK_RX_TRIG_16 (3 << 18)
  118. #define SLINK_RX_TRIG_MASK (3 << 18)
  119. #define SLINK_PACKED (1 << 20)
  120. #define SLINK_PACK_SIZE_4 (0 << 21)
  121. #define SLINK_PACK_SIZE_8 (1 << 21)
  122. #define SLINK_PACK_SIZE_16 (2 << 21)
  123. #define SLINK_PACK_SIZE_32 (3 << 21)
  124. #define SLINK_PACK_SIZE_MASK (3 << 21)
  125. #define SLINK_IE_TXC (1 << 26)
  126. #define SLINK_IE_RXC (1 << 27)
  127. #define SLINK_DMA_EN (1 << 31)
  128. #define SLINK_STATUS2 0x01c
  129. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  130. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
  131. #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
  132. #define SLINK_TX_FIFO 0x100
  133. #define SLINK_RX_FIFO 0x180
  134. #define DATA_DIR_TX (1 << 0)
  135. #define DATA_DIR_RX (1 << 1)
  136. #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
  137. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  138. #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  139. #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
  140. #define SLINK_STATUS2_RESET \
  141. (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  142. #define MAX_CHIP_SELECT 4
  143. #define SLINK_FIFO_DEPTH 32
  144. struct tegra_slink_chip_data {
  145. bool cs_hold_time;
  146. };
  147. struct tegra_slink_data {
  148. struct device *dev;
  149. struct spi_master *master;
  150. const struct tegra_slink_chip_data *chip_data;
  151. spinlock_t lock;
  152. struct clk *clk;
  153. void __iomem *base;
  154. phys_addr_t phys;
  155. unsigned irq;
  156. int dma_req_sel;
  157. u32 spi_max_frequency;
  158. u32 cur_speed;
  159. struct spi_device *cur_spi;
  160. unsigned cur_pos;
  161. unsigned cur_len;
  162. unsigned words_per_32bit;
  163. unsigned bytes_per_word;
  164. unsigned curr_dma_words;
  165. unsigned cur_direction;
  166. unsigned cur_rx_pos;
  167. unsigned cur_tx_pos;
  168. unsigned dma_buf_size;
  169. unsigned max_buf_size;
  170. bool is_curr_dma_xfer;
  171. bool is_hw_based_cs;
  172. struct completion rx_dma_complete;
  173. struct completion tx_dma_complete;
  174. u32 tx_status;
  175. u32 rx_status;
  176. u32 status_reg;
  177. bool is_packed;
  178. unsigned long packed_size;
  179. u32 command_reg;
  180. u32 command2_reg;
  181. u32 dma_control_reg;
  182. u32 def_command_reg;
  183. u32 def_command2_reg;
  184. struct completion xfer_completion;
  185. struct spi_transfer *curr_xfer;
  186. struct dma_chan *rx_dma_chan;
  187. u32 *rx_dma_buf;
  188. dma_addr_t rx_dma_phys;
  189. struct dma_async_tx_descriptor *rx_dma_desc;
  190. struct dma_chan *tx_dma_chan;
  191. u32 *tx_dma_buf;
  192. dma_addr_t tx_dma_phys;
  193. struct dma_async_tx_descriptor *tx_dma_desc;
  194. };
  195. static int tegra_slink_runtime_suspend(struct device *dev);
  196. static int tegra_slink_runtime_resume(struct device *dev);
  197. static inline unsigned long tegra_slink_readl(struct tegra_slink_data *tspi,
  198. unsigned long reg)
  199. {
  200. return readl(tspi->base + reg);
  201. }
  202. static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
  203. unsigned long val, unsigned long reg)
  204. {
  205. writel(val, tspi->base + reg);
  206. /* Read back register to make sure that register writes completed */
  207. if (reg != SLINK_TX_FIFO)
  208. readl(tspi->base + SLINK_MAS_DATA);
  209. }
  210. static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  211. {
  212. unsigned long val;
  213. unsigned long val_write = 0;
  214. val = tegra_slink_readl(tspi, SLINK_STATUS);
  215. /* Write 1 to clear status register */
  216. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  217. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  218. }
  219. static unsigned long tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  220. struct spi_transfer *t)
  221. {
  222. unsigned long val;
  223. switch (tspi->bytes_per_word) {
  224. case 0:
  225. val = SLINK_PACK_SIZE_4;
  226. break;
  227. case 1:
  228. val = SLINK_PACK_SIZE_8;
  229. break;
  230. case 2:
  231. val = SLINK_PACK_SIZE_16;
  232. break;
  233. case 4:
  234. val = SLINK_PACK_SIZE_32;
  235. break;
  236. default:
  237. val = 0;
  238. }
  239. return val;
  240. }
  241. static unsigned tegra_slink_calculate_curr_xfer_param(
  242. struct spi_device *spi, struct tegra_slink_data *tspi,
  243. struct spi_transfer *t)
  244. {
  245. unsigned remain_len = t->len - tspi->cur_pos;
  246. unsigned max_word;
  247. unsigned bits_per_word ;
  248. unsigned max_len;
  249. unsigned total_fifo_words;
  250. bits_per_word = t->bits_per_word;
  251. tspi->bytes_per_word = (bits_per_word - 1) / 8 + 1;
  252. if (bits_per_word == 8 || bits_per_word == 16) {
  253. tspi->is_packed = 1;
  254. tspi->words_per_32bit = 32/bits_per_word;
  255. } else {
  256. tspi->is_packed = 0;
  257. tspi->words_per_32bit = 1;
  258. }
  259. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  260. if (tspi->is_packed) {
  261. max_len = min(remain_len, tspi->max_buf_size);
  262. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  263. total_fifo_words = max_len/4;
  264. } else {
  265. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  266. max_word = min(max_word, tspi->max_buf_size/4);
  267. tspi->curr_dma_words = max_word;
  268. total_fifo_words = max_word;
  269. }
  270. return total_fifo_words;
  271. }
  272. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  273. struct tegra_slink_data *tspi, struct spi_transfer *t)
  274. {
  275. unsigned nbytes;
  276. unsigned tx_empty_count;
  277. unsigned long fifo_status;
  278. unsigned max_n_32bit;
  279. unsigned i, count;
  280. unsigned long x;
  281. unsigned int written_words;
  282. unsigned fifo_words_left;
  283. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  284. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  285. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  286. if (tspi->is_packed) {
  287. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  288. written_words = min(fifo_words_left, tspi->curr_dma_words);
  289. nbytes = written_words * tspi->bytes_per_word;
  290. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  291. for (count = 0; count < max_n_32bit; count++) {
  292. x = 0;
  293. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  294. x |= (*tx_buf++) << (i*8);
  295. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  296. }
  297. } else {
  298. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  299. written_words = max_n_32bit;
  300. nbytes = written_words * tspi->bytes_per_word;
  301. for (count = 0; count < max_n_32bit; count++) {
  302. x = 0;
  303. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  304. i++, nbytes--)
  305. x |= ((*tx_buf++) << i*8);
  306. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  307. }
  308. }
  309. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  310. return written_words;
  311. }
  312. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  313. struct tegra_slink_data *tspi, struct spi_transfer *t)
  314. {
  315. unsigned rx_full_count;
  316. unsigned long fifo_status;
  317. unsigned i, count;
  318. unsigned long x;
  319. unsigned int read_words = 0;
  320. unsigned len;
  321. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  322. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  323. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  324. if (tspi->is_packed) {
  325. len = tspi->curr_dma_words * tspi->bytes_per_word;
  326. for (count = 0; count < rx_full_count; count++) {
  327. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  328. for (i = 0; len && (i < 4); i++, len--)
  329. *rx_buf++ = (x >> i*8) & 0xFF;
  330. }
  331. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  332. read_words += tspi->curr_dma_words;
  333. } else {
  334. unsigned int bits_per_word;
  335. bits_per_word = t->bits_per_word;
  336. for (count = 0; count < rx_full_count; count++) {
  337. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  338. for (i = 0; (i < tspi->bytes_per_word); i++)
  339. *rx_buf++ = (x >> (i*8)) & 0xFF;
  340. }
  341. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  342. read_words += rx_full_count;
  343. }
  344. return read_words;
  345. }
  346. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  347. struct tegra_slink_data *tspi, struct spi_transfer *t)
  348. {
  349. unsigned len;
  350. /* Make the dma buffer to read by cpu */
  351. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  352. tspi->dma_buf_size, DMA_TO_DEVICE);
  353. if (tspi->is_packed) {
  354. len = tspi->curr_dma_words * tspi->bytes_per_word;
  355. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  356. } else {
  357. unsigned int i;
  358. unsigned int count;
  359. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  360. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  361. unsigned int x;
  362. for (count = 0; count < tspi->curr_dma_words; count++) {
  363. x = 0;
  364. for (i = 0; consume && (i < tspi->bytes_per_word);
  365. i++, consume--)
  366. x |= ((*tx_buf++) << i * 8);
  367. tspi->tx_dma_buf[count] = x;
  368. }
  369. }
  370. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  371. /* Make the dma buffer to read by dma */
  372. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  373. tspi->dma_buf_size, DMA_TO_DEVICE);
  374. }
  375. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  376. struct tegra_slink_data *tspi, struct spi_transfer *t)
  377. {
  378. unsigned len;
  379. /* Make the dma buffer to read by cpu */
  380. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  381. tspi->dma_buf_size, DMA_FROM_DEVICE);
  382. if (tspi->is_packed) {
  383. len = tspi->curr_dma_words * tspi->bytes_per_word;
  384. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  385. } else {
  386. unsigned int i;
  387. unsigned int count;
  388. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  389. unsigned int x;
  390. unsigned int rx_mask, bits_per_word;
  391. bits_per_word = t->bits_per_word;
  392. rx_mask = (1 << bits_per_word) - 1;
  393. for (count = 0; count < tspi->curr_dma_words; count++) {
  394. x = tspi->rx_dma_buf[count];
  395. x &= rx_mask;
  396. for (i = 0; (i < tspi->bytes_per_word); i++)
  397. *rx_buf++ = (x >> (i*8)) & 0xFF;
  398. }
  399. }
  400. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  401. /* Make the dma buffer to read by dma */
  402. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  403. tspi->dma_buf_size, DMA_FROM_DEVICE);
  404. }
  405. static void tegra_slink_dma_complete(void *args)
  406. {
  407. struct completion *dma_complete = args;
  408. complete(dma_complete);
  409. }
  410. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  411. {
  412. INIT_COMPLETION(tspi->tx_dma_complete);
  413. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  414. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  415. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  416. if (!tspi->tx_dma_desc) {
  417. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  418. return -EIO;
  419. }
  420. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  421. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  422. dmaengine_submit(tspi->tx_dma_desc);
  423. dma_async_issue_pending(tspi->tx_dma_chan);
  424. return 0;
  425. }
  426. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  427. {
  428. INIT_COMPLETION(tspi->rx_dma_complete);
  429. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  430. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  431. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  432. if (!tspi->rx_dma_desc) {
  433. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  434. return -EIO;
  435. }
  436. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  437. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  438. dmaengine_submit(tspi->rx_dma_desc);
  439. dma_async_issue_pending(tspi->rx_dma_chan);
  440. return 0;
  441. }
  442. static int tegra_slink_start_dma_based_transfer(
  443. struct tegra_slink_data *tspi, struct spi_transfer *t)
  444. {
  445. unsigned long val;
  446. unsigned long test_val;
  447. unsigned int len;
  448. int ret = 0;
  449. unsigned long status;
  450. /* Make sure that Rx and Tx fifo are empty */
  451. status = tegra_slink_readl(tspi, SLINK_STATUS);
  452. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  453. dev_err(tspi->dev,
  454. "Rx/Tx fifo are not empty status 0x%08lx\n", status);
  455. return -EIO;
  456. }
  457. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  458. val |= tspi->packed_size;
  459. if (tspi->is_packed)
  460. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  461. 4) * 4;
  462. else
  463. len = tspi->curr_dma_words * 4;
  464. /* Set attention level based on length of transfer */
  465. if (len & 0xF)
  466. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  467. else if (((len) >> 4) & 0x1)
  468. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  469. else
  470. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  471. if (tspi->cur_direction & DATA_DIR_TX)
  472. val |= SLINK_IE_TXC;
  473. if (tspi->cur_direction & DATA_DIR_RX)
  474. val |= SLINK_IE_RXC;
  475. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  476. tspi->dma_control_reg = val;
  477. if (tspi->cur_direction & DATA_DIR_TX) {
  478. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  479. wmb();
  480. ret = tegra_slink_start_tx_dma(tspi, len);
  481. if (ret < 0) {
  482. dev_err(tspi->dev,
  483. "Starting tx dma failed, err %d\n", ret);
  484. return ret;
  485. }
  486. /* Wait for tx fifo to be fill before starting slink */
  487. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  488. while (!(test_val & SLINK_TX_FULL))
  489. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  490. }
  491. if (tspi->cur_direction & DATA_DIR_RX) {
  492. /* Make the dma buffer to read by dma */
  493. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  494. tspi->dma_buf_size, DMA_FROM_DEVICE);
  495. ret = tegra_slink_start_rx_dma(tspi, len);
  496. if (ret < 0) {
  497. dev_err(tspi->dev,
  498. "Starting rx dma failed, err %d\n", ret);
  499. if (tspi->cur_direction & DATA_DIR_TX)
  500. dmaengine_terminate_all(tspi->tx_dma_chan);
  501. return ret;
  502. }
  503. }
  504. tspi->is_curr_dma_xfer = true;
  505. if (tspi->is_packed) {
  506. val |= SLINK_PACKED;
  507. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  508. /* HW need small delay after settign Packed mode */
  509. udelay(1);
  510. }
  511. tspi->dma_control_reg = val;
  512. val |= SLINK_DMA_EN;
  513. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  514. return ret;
  515. }
  516. static int tegra_slink_start_cpu_based_transfer(
  517. struct tegra_slink_data *tspi, struct spi_transfer *t)
  518. {
  519. unsigned long val;
  520. unsigned cur_words;
  521. val = tspi->packed_size;
  522. if (tspi->cur_direction & DATA_DIR_TX)
  523. val |= SLINK_IE_TXC;
  524. if (tspi->cur_direction & DATA_DIR_RX)
  525. val |= SLINK_IE_RXC;
  526. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  527. tspi->dma_control_reg = val;
  528. if (tspi->cur_direction & DATA_DIR_TX)
  529. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  530. else
  531. cur_words = tspi->curr_dma_words;
  532. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  533. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  534. tspi->dma_control_reg = val;
  535. tspi->is_curr_dma_xfer = false;
  536. if (tspi->is_packed) {
  537. val |= SLINK_PACKED;
  538. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  539. udelay(1);
  540. wmb();
  541. }
  542. tspi->dma_control_reg = val;
  543. val |= SLINK_DMA_EN;
  544. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  545. return 0;
  546. }
  547. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  548. bool dma_to_memory)
  549. {
  550. struct dma_chan *dma_chan;
  551. u32 *dma_buf;
  552. dma_addr_t dma_phys;
  553. int ret;
  554. struct dma_slave_config dma_sconfig;
  555. dma_cap_mask_t mask;
  556. dma_cap_zero(mask);
  557. dma_cap_set(DMA_SLAVE, mask);
  558. dma_chan = dma_request_channel(mask, NULL, NULL);
  559. if (!dma_chan) {
  560. dev_err(tspi->dev,
  561. "Dma channel is not available, will try later\n");
  562. return -EPROBE_DEFER;
  563. }
  564. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  565. &dma_phys, GFP_KERNEL);
  566. if (!dma_buf) {
  567. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  568. dma_release_channel(dma_chan);
  569. return -ENOMEM;
  570. }
  571. dma_sconfig.slave_id = tspi->dma_req_sel;
  572. if (dma_to_memory) {
  573. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  574. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  575. dma_sconfig.src_maxburst = 0;
  576. } else {
  577. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  578. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  579. dma_sconfig.dst_maxburst = 0;
  580. }
  581. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  582. if (ret)
  583. goto scrub;
  584. if (dma_to_memory) {
  585. tspi->rx_dma_chan = dma_chan;
  586. tspi->rx_dma_buf = dma_buf;
  587. tspi->rx_dma_phys = dma_phys;
  588. } else {
  589. tspi->tx_dma_chan = dma_chan;
  590. tspi->tx_dma_buf = dma_buf;
  591. tspi->tx_dma_phys = dma_phys;
  592. }
  593. return 0;
  594. scrub:
  595. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  596. dma_release_channel(dma_chan);
  597. return ret;
  598. }
  599. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  600. bool dma_to_memory)
  601. {
  602. u32 *dma_buf;
  603. dma_addr_t dma_phys;
  604. struct dma_chan *dma_chan;
  605. if (dma_to_memory) {
  606. dma_buf = tspi->rx_dma_buf;
  607. dma_chan = tspi->rx_dma_chan;
  608. dma_phys = tspi->rx_dma_phys;
  609. tspi->rx_dma_chan = NULL;
  610. tspi->rx_dma_buf = NULL;
  611. } else {
  612. dma_buf = tspi->tx_dma_buf;
  613. dma_chan = tspi->tx_dma_chan;
  614. dma_phys = tspi->tx_dma_phys;
  615. tspi->tx_dma_buf = NULL;
  616. tspi->tx_dma_chan = NULL;
  617. }
  618. if (!dma_chan)
  619. return;
  620. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  621. dma_release_channel(dma_chan);
  622. }
  623. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  624. struct spi_transfer *t, bool is_first_of_msg,
  625. bool is_single_xfer)
  626. {
  627. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  628. u32 speed;
  629. u8 bits_per_word;
  630. unsigned total_fifo_words;
  631. int ret;
  632. struct tegra_spi_device_controller_data *cdata = spi->controller_data;
  633. unsigned long command;
  634. unsigned long command2;
  635. bits_per_word = t->bits_per_word;
  636. speed = t->speed_hz;
  637. if (speed != tspi->cur_speed) {
  638. clk_set_rate(tspi->clk, speed * 4);
  639. tspi->cur_speed = speed;
  640. }
  641. tspi->cur_spi = spi;
  642. tspi->cur_pos = 0;
  643. tspi->cur_rx_pos = 0;
  644. tspi->cur_tx_pos = 0;
  645. tspi->curr_xfer = t;
  646. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  647. if (is_first_of_msg) {
  648. tegra_slink_clear_status(tspi);
  649. command = tspi->def_command_reg;
  650. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  651. command2 = tspi->def_command2_reg;
  652. command2 |= SLINK_SS_EN_CS(spi->chip_select);
  653. /* possibly use the hw based chip select */
  654. tspi->is_hw_based_cs = false;
  655. if (cdata && cdata->is_hw_based_cs && is_single_xfer &&
  656. ((tspi->curr_dma_words * tspi->bytes_per_word) ==
  657. (t->len - tspi->cur_pos))) {
  658. int setup_count;
  659. int sts2;
  660. setup_count = cdata->cs_setup_clk_count >> 1;
  661. setup_count = max(setup_count, 3);
  662. command2 |= SLINK_SS_SETUP(setup_count);
  663. if (tspi->chip_data->cs_hold_time) {
  664. int hold_count;
  665. hold_count = cdata->cs_hold_clk_count;
  666. hold_count = max(hold_count, 0xF);
  667. sts2 = tegra_slink_readl(tspi, SLINK_STATUS2);
  668. sts2 &= ~SLINK_SS_HOLD_TIME(0xF);
  669. sts2 |= SLINK_SS_HOLD_TIME(hold_count);
  670. tegra_slink_writel(tspi, sts2, SLINK_STATUS2);
  671. }
  672. tspi->is_hw_based_cs = true;
  673. }
  674. if (tspi->is_hw_based_cs)
  675. command &= ~SLINK_CS_SW;
  676. else
  677. command |= SLINK_CS_SW | SLINK_CS_VALUE;
  678. command &= ~SLINK_MODES;
  679. if (spi->mode & SPI_CPHA)
  680. command |= SLINK_CK_SDA;
  681. if (spi->mode & SPI_CPOL)
  682. command |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  683. else
  684. command |= SLINK_IDLE_SCLK_DRIVE_LOW;
  685. } else {
  686. command = tspi->command_reg;
  687. command &= ~SLINK_BIT_LENGTH(~0);
  688. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  689. command2 = tspi->command2_reg;
  690. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  691. }
  692. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  693. tspi->command_reg = command;
  694. tspi->cur_direction = 0;
  695. if (t->rx_buf) {
  696. command2 |= SLINK_RXEN;
  697. tspi->cur_direction |= DATA_DIR_RX;
  698. }
  699. if (t->tx_buf) {
  700. command2 |= SLINK_TXEN;
  701. tspi->cur_direction |= DATA_DIR_TX;
  702. }
  703. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  704. tspi->command2_reg = command2;
  705. if (total_fifo_words > SLINK_FIFO_DEPTH)
  706. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  707. else
  708. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  709. return ret;
  710. }
  711. static int tegra_slink_setup(struct spi_device *spi)
  712. {
  713. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  714. unsigned long val;
  715. unsigned long flags;
  716. int ret;
  717. unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
  718. SLINK_CS_POLARITY,
  719. SLINK_CS_POLARITY1,
  720. SLINK_CS_POLARITY2,
  721. SLINK_CS_POLARITY3,
  722. };
  723. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  724. spi->bits_per_word,
  725. spi->mode & SPI_CPOL ? "" : "~",
  726. spi->mode & SPI_CPHA ? "" : "~",
  727. spi->max_speed_hz);
  728. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  729. /* Set speed to the spi max fequency if spi device has not set */
  730. spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
  731. ret = pm_runtime_get_sync(tspi->dev);
  732. if (ret < 0) {
  733. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  734. return ret;
  735. }
  736. spin_lock_irqsave(&tspi->lock, flags);
  737. val = tspi->def_command_reg;
  738. if (spi->mode & SPI_CS_HIGH)
  739. val |= cs_pol_bit[spi->chip_select];
  740. else
  741. val &= ~cs_pol_bit[spi->chip_select];
  742. tspi->def_command_reg = val;
  743. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  744. spin_unlock_irqrestore(&tspi->lock, flags);
  745. pm_runtime_put(tspi->dev);
  746. return 0;
  747. }
  748. static int tegra_slink_transfer_one_message(struct spi_master *master,
  749. struct spi_message *msg)
  750. {
  751. bool is_first_msg = true;
  752. int single_xfer;
  753. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  754. struct spi_transfer *xfer;
  755. struct spi_device *spi = msg->spi;
  756. int ret;
  757. msg->status = 0;
  758. msg->actual_length = 0;
  759. ret = pm_runtime_get_sync(tspi->dev);
  760. if (ret < 0) {
  761. dev_err(tspi->dev, "runtime get failed: %d\n", ret);
  762. goto done;
  763. }
  764. single_xfer = list_is_singular(&msg->transfers);
  765. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  766. INIT_COMPLETION(tspi->xfer_completion);
  767. ret = tegra_slink_start_transfer_one(spi, xfer,
  768. is_first_msg, single_xfer);
  769. if (ret < 0) {
  770. dev_err(tspi->dev,
  771. "spi can not start transfer, err %d\n", ret);
  772. goto exit;
  773. }
  774. is_first_msg = false;
  775. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  776. SLINK_DMA_TIMEOUT);
  777. if (WARN_ON(ret == 0)) {
  778. dev_err(tspi->dev,
  779. "spi trasfer timeout, err %d\n", ret);
  780. ret = -EIO;
  781. goto exit;
  782. }
  783. if (tspi->tx_status || tspi->rx_status) {
  784. dev_err(tspi->dev, "Error in Transfer\n");
  785. ret = -EIO;
  786. goto exit;
  787. }
  788. msg->actual_length += xfer->len;
  789. if (xfer->cs_change && xfer->delay_usecs) {
  790. tegra_slink_writel(tspi, tspi->def_command_reg,
  791. SLINK_COMMAND);
  792. udelay(xfer->delay_usecs);
  793. }
  794. }
  795. ret = 0;
  796. exit:
  797. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  798. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  799. pm_runtime_put(tspi->dev);
  800. done:
  801. msg->status = ret;
  802. spi_finalize_current_message(master);
  803. return ret;
  804. }
  805. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  806. {
  807. struct spi_transfer *t = tspi->curr_xfer;
  808. unsigned long flags;
  809. spin_lock_irqsave(&tspi->lock, flags);
  810. if (tspi->tx_status || tspi->rx_status ||
  811. (tspi->status_reg & SLINK_BSY)) {
  812. dev_err(tspi->dev,
  813. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  814. dev_err(tspi->dev,
  815. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  816. tspi->command2_reg, tspi->dma_control_reg);
  817. tegra_periph_reset_assert(tspi->clk);
  818. udelay(2);
  819. tegra_periph_reset_deassert(tspi->clk);
  820. complete(&tspi->xfer_completion);
  821. goto exit;
  822. }
  823. if (tspi->cur_direction & DATA_DIR_RX)
  824. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  825. if (tspi->cur_direction & DATA_DIR_TX)
  826. tspi->cur_pos = tspi->cur_tx_pos;
  827. else
  828. tspi->cur_pos = tspi->cur_rx_pos;
  829. if (tspi->cur_pos == t->len) {
  830. complete(&tspi->xfer_completion);
  831. goto exit;
  832. }
  833. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  834. tegra_slink_start_cpu_based_transfer(tspi, t);
  835. exit:
  836. spin_unlock_irqrestore(&tspi->lock, flags);
  837. return IRQ_HANDLED;
  838. }
  839. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  840. {
  841. struct spi_transfer *t = tspi->curr_xfer;
  842. long wait_status;
  843. int err = 0;
  844. unsigned total_fifo_words;
  845. unsigned long flags;
  846. /* Abort dmas if any error */
  847. if (tspi->cur_direction & DATA_DIR_TX) {
  848. if (tspi->tx_status) {
  849. dmaengine_terminate_all(tspi->tx_dma_chan);
  850. err += 1;
  851. } else {
  852. wait_status = wait_for_completion_interruptible_timeout(
  853. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  854. if (wait_status <= 0) {
  855. dmaengine_terminate_all(tspi->tx_dma_chan);
  856. dev_err(tspi->dev, "TxDma Xfer failed\n");
  857. err += 1;
  858. }
  859. }
  860. }
  861. if (tspi->cur_direction & DATA_DIR_RX) {
  862. if (tspi->rx_status) {
  863. dmaengine_terminate_all(tspi->rx_dma_chan);
  864. err += 2;
  865. } else {
  866. wait_status = wait_for_completion_interruptible_timeout(
  867. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  868. if (wait_status <= 0) {
  869. dmaengine_terminate_all(tspi->rx_dma_chan);
  870. dev_err(tspi->dev, "RxDma Xfer failed\n");
  871. err += 2;
  872. }
  873. }
  874. }
  875. spin_lock_irqsave(&tspi->lock, flags);
  876. if (err) {
  877. dev_err(tspi->dev,
  878. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  879. dev_err(tspi->dev,
  880. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  881. tspi->command2_reg, tspi->dma_control_reg);
  882. tegra_periph_reset_assert(tspi->clk);
  883. udelay(2);
  884. tegra_periph_reset_deassert(tspi->clk);
  885. complete(&tspi->xfer_completion);
  886. spin_unlock_irqrestore(&tspi->lock, flags);
  887. return IRQ_HANDLED;
  888. }
  889. if (tspi->cur_direction & DATA_DIR_RX)
  890. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  891. if (tspi->cur_direction & DATA_DIR_TX)
  892. tspi->cur_pos = tspi->cur_tx_pos;
  893. else
  894. tspi->cur_pos = tspi->cur_rx_pos;
  895. if (tspi->cur_pos == t->len) {
  896. complete(&tspi->xfer_completion);
  897. goto exit;
  898. }
  899. /* Continue transfer in current message */
  900. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  901. tspi, t);
  902. if (total_fifo_words > SLINK_FIFO_DEPTH)
  903. err = tegra_slink_start_dma_based_transfer(tspi, t);
  904. else
  905. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  906. exit:
  907. spin_unlock_irqrestore(&tspi->lock, flags);
  908. return IRQ_HANDLED;
  909. }
  910. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  911. {
  912. struct tegra_slink_data *tspi = context_data;
  913. if (!tspi->is_curr_dma_xfer)
  914. return handle_cpu_based_xfer(tspi);
  915. return handle_dma_based_xfer(tspi);
  916. }
  917. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  918. {
  919. struct tegra_slink_data *tspi = context_data;
  920. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  921. if (tspi->cur_direction & DATA_DIR_TX)
  922. tspi->tx_status = tspi->status_reg &
  923. (SLINK_TX_OVF | SLINK_TX_UNF);
  924. if (tspi->cur_direction & DATA_DIR_RX)
  925. tspi->rx_status = tspi->status_reg &
  926. (SLINK_RX_OVF | SLINK_RX_UNF);
  927. tegra_slink_clear_status(tspi);
  928. return IRQ_WAKE_THREAD;
  929. }
  930. static struct tegra_spi_platform_data *tegra_slink_parse_dt(
  931. struct platform_device *pdev)
  932. {
  933. struct tegra_spi_platform_data *pdata;
  934. const unsigned int *prop;
  935. struct device_node *np = pdev->dev.of_node;
  936. u32 of_dma[2];
  937. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  938. if (!pdata) {
  939. dev_err(&pdev->dev, "Memory alloc for pdata failed\n");
  940. return NULL;
  941. }
  942. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  943. of_dma, 2) >= 0)
  944. pdata->dma_req_sel = of_dma[1];
  945. prop = of_get_property(np, "spi-max-frequency", NULL);
  946. if (prop)
  947. pdata->spi_max_frequency = be32_to_cpup(prop);
  948. return pdata;
  949. }
  950. const struct tegra_slink_chip_data tegra30_spi_cdata = {
  951. .cs_hold_time = true,
  952. };
  953. const struct tegra_slink_chip_data tegra20_spi_cdata = {
  954. .cs_hold_time = false,
  955. };
  956. static struct of_device_id tegra_slink_of_match[] = {
  957. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  958. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  959. {}
  960. };
  961. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  962. static int tegra_slink_probe(struct platform_device *pdev)
  963. {
  964. struct spi_master *master;
  965. struct tegra_slink_data *tspi;
  966. struct resource *r;
  967. struct tegra_spi_platform_data *pdata = pdev->dev.platform_data;
  968. int ret, spi_irq;
  969. const struct tegra_slink_chip_data *cdata = NULL;
  970. const struct of_device_id *match;
  971. match = of_match_device(of_match_ptr(tegra_slink_of_match), &pdev->dev);
  972. if (!match) {
  973. dev_err(&pdev->dev, "Error: No device match found\n");
  974. return -ENODEV;
  975. }
  976. cdata = match->data;
  977. if (!pdata && pdev->dev.of_node)
  978. pdata = tegra_slink_parse_dt(pdev);
  979. if (!pdata) {
  980. dev_err(&pdev->dev, "No platform data, exiting\n");
  981. return -ENODEV;
  982. }
  983. if (!pdata->spi_max_frequency)
  984. pdata->spi_max_frequency = 25000000; /* 25MHz */
  985. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  986. if (!master) {
  987. dev_err(&pdev->dev, "master allocation failed\n");
  988. return -ENOMEM;
  989. }
  990. /* the spi->mode bits understood by this driver: */
  991. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  992. master->setup = tegra_slink_setup;
  993. master->transfer_one_message = tegra_slink_transfer_one_message;
  994. master->num_chipselect = MAX_CHIP_SELECT;
  995. master->bus_num = -1;
  996. dev_set_drvdata(&pdev->dev, master);
  997. tspi = spi_master_get_devdata(master);
  998. tspi->master = master;
  999. tspi->dma_req_sel = pdata->dma_req_sel;
  1000. tspi->dev = &pdev->dev;
  1001. tspi->chip_data = cdata;
  1002. spin_lock_init(&tspi->lock);
  1003. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1004. if (!r) {
  1005. dev_err(&pdev->dev, "No IO memory resource\n");
  1006. ret = -ENODEV;
  1007. goto exit_free_master;
  1008. }
  1009. tspi->phys = r->start;
  1010. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  1011. if (IS_ERR(tspi->base)) {
  1012. ret = PTR_ERR(tspi->base);
  1013. goto exit_free_master;
  1014. }
  1015. spi_irq = platform_get_irq(pdev, 0);
  1016. tspi->irq = spi_irq;
  1017. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  1018. tegra_slink_isr_thread, IRQF_ONESHOT,
  1019. dev_name(&pdev->dev), tspi);
  1020. if (ret < 0) {
  1021. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  1022. tspi->irq);
  1023. goto exit_free_master;
  1024. }
  1025. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  1026. if (IS_ERR(tspi->clk)) {
  1027. dev_err(&pdev->dev, "can not get clock\n");
  1028. ret = PTR_ERR(tspi->clk);
  1029. goto exit_free_irq;
  1030. }
  1031. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  1032. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  1033. tspi->spi_max_frequency = pdata->spi_max_frequency;
  1034. if (pdata->dma_req_sel) {
  1035. ret = tegra_slink_init_dma_param(tspi, true);
  1036. if (ret < 0) {
  1037. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  1038. goto exit_free_irq;
  1039. }
  1040. ret = tegra_slink_init_dma_param(tspi, false);
  1041. if (ret < 0) {
  1042. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  1043. goto exit_rx_dma_free;
  1044. }
  1045. tspi->max_buf_size = tspi->dma_buf_size;
  1046. init_completion(&tspi->tx_dma_complete);
  1047. init_completion(&tspi->rx_dma_complete);
  1048. }
  1049. init_completion(&tspi->xfer_completion);
  1050. pm_runtime_enable(&pdev->dev);
  1051. if (!pm_runtime_enabled(&pdev->dev)) {
  1052. ret = tegra_slink_runtime_resume(&pdev->dev);
  1053. if (ret)
  1054. goto exit_pm_disable;
  1055. }
  1056. ret = pm_runtime_get_sync(&pdev->dev);
  1057. if (ret < 0) {
  1058. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  1059. goto exit_pm_disable;
  1060. }
  1061. tspi->def_command_reg = SLINK_M_S;
  1062. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  1063. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  1064. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  1065. pm_runtime_put(&pdev->dev);
  1066. master->dev.of_node = pdev->dev.of_node;
  1067. ret = spi_register_master(master);
  1068. if (ret < 0) {
  1069. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1070. goto exit_pm_disable;
  1071. }
  1072. return ret;
  1073. exit_pm_disable:
  1074. pm_runtime_disable(&pdev->dev);
  1075. if (!pm_runtime_status_suspended(&pdev->dev))
  1076. tegra_slink_runtime_suspend(&pdev->dev);
  1077. tegra_slink_deinit_dma_param(tspi, false);
  1078. exit_rx_dma_free:
  1079. tegra_slink_deinit_dma_param(tspi, true);
  1080. exit_free_irq:
  1081. free_irq(spi_irq, tspi);
  1082. exit_free_master:
  1083. spi_master_put(master);
  1084. return ret;
  1085. }
  1086. static int tegra_slink_remove(struct platform_device *pdev)
  1087. {
  1088. struct spi_master *master = dev_get_drvdata(&pdev->dev);
  1089. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1090. free_irq(tspi->irq, tspi);
  1091. spi_unregister_master(master);
  1092. if (tspi->tx_dma_chan)
  1093. tegra_slink_deinit_dma_param(tspi, false);
  1094. if (tspi->rx_dma_chan)
  1095. tegra_slink_deinit_dma_param(tspi, true);
  1096. pm_runtime_disable(&pdev->dev);
  1097. if (!pm_runtime_status_suspended(&pdev->dev))
  1098. tegra_slink_runtime_suspend(&pdev->dev);
  1099. return 0;
  1100. }
  1101. #ifdef CONFIG_PM_SLEEP
  1102. static int tegra_slink_suspend(struct device *dev)
  1103. {
  1104. struct spi_master *master = dev_get_drvdata(dev);
  1105. return spi_master_suspend(master);
  1106. }
  1107. static int tegra_slink_resume(struct device *dev)
  1108. {
  1109. struct spi_master *master = dev_get_drvdata(dev);
  1110. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1111. int ret;
  1112. ret = pm_runtime_get_sync(dev);
  1113. if (ret < 0) {
  1114. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1115. return ret;
  1116. }
  1117. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1118. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1119. pm_runtime_put(dev);
  1120. return spi_master_resume(master);
  1121. }
  1122. #endif
  1123. static int tegra_slink_runtime_suspend(struct device *dev)
  1124. {
  1125. struct spi_master *master = dev_get_drvdata(dev);
  1126. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1127. /* Flush all write which are in PPSB queue by reading back */
  1128. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1129. clk_disable_unprepare(tspi->clk);
  1130. return 0;
  1131. }
  1132. static int tegra_slink_runtime_resume(struct device *dev)
  1133. {
  1134. struct spi_master *master = dev_get_drvdata(dev);
  1135. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1136. int ret;
  1137. ret = clk_prepare_enable(tspi->clk);
  1138. if (ret < 0) {
  1139. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1140. return ret;
  1141. }
  1142. return 0;
  1143. }
  1144. static const struct dev_pm_ops slink_pm_ops = {
  1145. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1146. tegra_slink_runtime_resume, NULL)
  1147. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1148. };
  1149. static struct platform_driver tegra_slink_driver = {
  1150. .driver = {
  1151. .name = "spi-tegra-slink",
  1152. .owner = THIS_MODULE,
  1153. .pm = &slink_pm_ops,
  1154. .of_match_table = of_match_ptr(tegra_slink_of_match),
  1155. },
  1156. .probe = tegra_slink_probe,
  1157. .remove = tegra_slink_remove,
  1158. };
  1159. module_platform_driver(tegra_slink_driver);
  1160. MODULE_ALIAS("platform:spi-tegra-slink");
  1161. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1162. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1163. MODULE_LICENSE("GPL v2");