blob: af78c17388d24097bd23d8b5b99a45868d41b12a [file] [log] [blame]
Laxman Dewangan85285472012-11-14 05:54:47 +05301/*
2 * SPI driver for Nvidia's Tegra20 Serial Flash Controller.
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 *
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/clk.h>
22#include <linux/completion.h>
23#include <linux/delay.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>
Prashant Gaikwad61fd2902013-01-11 13:16:26 +053036#include <linux/clk/tegra.h>
Laxman Dewangan85285472012-11-14 05:54:47 +053037
38#define SPI_COMMAND 0x000
39#define SPI_GO BIT(30)
40#define SPI_M_S BIT(28)
41#define SPI_ACTIVE_SCLK_MASK (0x3 << 26)
42#define SPI_ACTIVE_SCLK_DRIVE_LOW (0 << 26)
43#define SPI_ACTIVE_SCLK_DRIVE_HIGH (1 << 26)
44#define SPI_ACTIVE_SCLK_PULL_LOW (2 << 26)
45#define SPI_ACTIVE_SCLK_PULL_HIGH (3 << 26)
46
47#define SPI_CK_SDA_FALLING (1 << 21)
48#define SPI_CK_SDA_RISING (0 << 21)
49#define SPI_CK_SDA_MASK (1 << 21)
50#define SPI_ACTIVE_SDA (0x3 << 18)
51#define SPI_ACTIVE_SDA_DRIVE_LOW (0 << 18)
52#define SPI_ACTIVE_SDA_DRIVE_HIGH (1 << 18)
53#define SPI_ACTIVE_SDA_PULL_LOW (2 << 18)
54#define SPI_ACTIVE_SDA_PULL_HIGH (3 << 18)
55
56#define SPI_CS_POL_INVERT BIT(16)
57#define SPI_TX_EN BIT(15)
58#define SPI_RX_EN BIT(14)
59#define SPI_CS_VAL_HIGH BIT(13)
60#define SPI_CS_VAL_LOW 0x0
61#define SPI_CS_SW BIT(12)
62#define SPI_CS_HW 0x0
63#define SPI_CS_DELAY_MASK (7 << 9)
64#define SPI_CS3_EN BIT(8)
65#define SPI_CS2_EN BIT(7)
66#define SPI_CS1_EN BIT(6)
67#define SPI_CS0_EN BIT(5)
68
69#define SPI_CS_MASK (SPI_CS3_EN | SPI_CS2_EN | \
70 SPI_CS1_EN | SPI_CS0_EN)
71#define SPI_BIT_LENGTH(x) (((x) & 0x1f) << 0)
72
73#define SPI_MODES (SPI_ACTIVE_SCLK_MASK | SPI_CK_SDA_MASK)
74
75#define SPI_STATUS 0x004
76#define SPI_BSY BIT(31)
77#define SPI_RDY BIT(30)
78#define SPI_TXF_FLUSH BIT(29)
79#define SPI_RXF_FLUSH BIT(28)
80#define SPI_RX_UNF BIT(27)
81#define SPI_TX_OVF BIT(26)
82#define SPI_RXF_EMPTY BIT(25)
83#define SPI_RXF_FULL BIT(24)
84#define SPI_TXF_EMPTY BIT(23)
85#define SPI_TXF_FULL BIT(22)
86#define SPI_BLK_CNT(count) (((count) & 0xffff) + 1)
87
88#define SPI_FIFO_ERROR (SPI_RX_UNF | SPI_TX_OVF)
89#define SPI_FIFO_EMPTY (SPI_TX_EMPTY | SPI_RX_EMPTY)
90
91#define SPI_RX_CMP 0x8
92#define SPI_DMA_CTL 0x0C
93#define SPI_DMA_EN BIT(31)
94#define SPI_IE_RXC BIT(27)
95#define SPI_IE_TXC BIT(26)
96#define SPI_PACKED BIT(20)
97#define SPI_RX_TRIG_MASK (0x3 << 18)
98#define SPI_RX_TRIG_1W (0x0 << 18)
99#define SPI_RX_TRIG_4W (0x1 << 18)
100#define SPI_TX_TRIG_MASK (0x3 << 16)
101#define SPI_TX_TRIG_1W (0x0 << 16)
102#define SPI_TX_TRIG_4W (0x1 << 16)
103#define SPI_DMA_BLK_COUNT(count) (((count) - 1) & 0xFFFF);
104
105#define SPI_TX_FIFO 0x10
106#define SPI_RX_FIFO 0x20
107
108#define DATA_DIR_TX (1 << 0)
109#define DATA_DIR_RX (1 << 1)
110
111#define MAX_CHIP_SELECT 4
112#define SPI_FIFO_DEPTH 4
113#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
114
115struct tegra_sflash_data {
116 struct device *dev;
117 struct spi_master *master;
118 spinlock_t lock;
119
120 struct clk *clk;
121 void __iomem *base;
122 unsigned irq;
123 u32 spi_max_frequency;
124 u32 cur_speed;
125
126 struct spi_device *cur_spi;
127 unsigned cur_pos;
128 unsigned cur_len;
129 unsigned bytes_per_word;
130 unsigned cur_direction;
131 unsigned curr_xfer_words;
132
133 unsigned cur_rx_pos;
134 unsigned cur_tx_pos;
135
136 u32 tx_status;
137 u32 rx_status;
138 u32 status_reg;
139
140 u32 def_command_reg;
141 u32 command_reg;
142 u32 dma_control_reg;
143
144 struct completion xfer_completion;
145 struct spi_transfer *curr_xfer;
146};
147
148static int tegra_sflash_runtime_suspend(struct device *dev);
149static int tegra_sflash_runtime_resume(struct device *dev);
150
Michal Nazarewicza553a312013-12-08 16:35:11 +0100151static inline u32 tegra_sflash_readl(struct tegra_sflash_data *tsd,
Laxman Dewangan85285472012-11-14 05:54:47 +0530152 unsigned long reg)
153{
154 return readl(tsd->base + reg);
155}
156
157static inline void tegra_sflash_writel(struct tegra_sflash_data *tsd,
Michal Nazarewicza553a312013-12-08 16:35:11 +0100158 u32 val, unsigned long reg)
Laxman Dewangan85285472012-11-14 05:54:47 +0530159{
160 writel(val, tsd->base + reg);
161}
162
163static void tegra_sflash_clear_status(struct tegra_sflash_data *tsd)
164{
165 /* Write 1 to clear status register */
166 tegra_sflash_writel(tsd, SPI_RDY | SPI_FIFO_ERROR, SPI_STATUS);
167}
168
169static unsigned tegra_sflash_calculate_curr_xfer_param(
170 struct spi_device *spi, struct tegra_sflash_data *tsd,
171 struct spi_transfer *t)
172{
173 unsigned remain_len = t->len - tsd->cur_pos;
174 unsigned max_word;
175
Axel Line91d2352013-08-30 11:00:23 +0800176 tsd->bytes_per_word = DIV_ROUND_UP(t->bits_per_word, 8);
Laxman Dewangan85285472012-11-14 05:54:47 +0530177 max_word = remain_len / tsd->bytes_per_word;
178 if (max_word > SPI_FIFO_DEPTH)
179 max_word = SPI_FIFO_DEPTH;
180 tsd->curr_xfer_words = max_word;
181 return max_word;
182}
183
184static unsigned tegra_sflash_fill_tx_fifo_from_client_txbuf(
185 struct tegra_sflash_data *tsd, struct spi_transfer *t)
186{
187 unsigned nbytes;
Michal Nazarewicza553a312013-12-08 16:35:11 +0100188 u32 status;
Laxman Dewangan85285472012-11-14 05:54:47 +0530189 unsigned max_n_32bit = tsd->curr_xfer_words;
190 u8 *tx_buf = (u8 *)t->tx_buf + tsd->cur_tx_pos;
191
192 if (max_n_32bit > SPI_FIFO_DEPTH)
193 max_n_32bit = SPI_FIFO_DEPTH;
194 nbytes = max_n_32bit * tsd->bytes_per_word;
195
196 status = tegra_sflash_readl(tsd, SPI_STATUS);
197 while (!(status & SPI_TXF_FULL)) {
198 int i;
Michal Nazarewicza553a312013-12-08 16:35:11 +0100199 u32 x = 0;
Laxman Dewangan85285472012-11-14 05:54:47 +0530200
201 for (i = 0; nbytes && (i < tsd->bytes_per_word);
202 i++, nbytes--)
Michal Nazarewicza553a312013-12-08 16:35:11 +0100203 x |= (u32)(*tx_buf++) << (i * 8);
Laxman Dewangan85285472012-11-14 05:54:47 +0530204 tegra_sflash_writel(tsd, x, SPI_TX_FIFO);
205 if (!nbytes)
206 break;
207
208 status = tegra_sflash_readl(tsd, SPI_STATUS);
209 }
210 tsd->cur_tx_pos += max_n_32bit * tsd->bytes_per_word;
211 return max_n_32bit;
212}
213
214static int tegra_sflash_read_rx_fifo_to_client_rxbuf(
215 struct tegra_sflash_data *tsd, struct spi_transfer *t)
216{
Michal Nazarewicza553a312013-12-08 16:35:11 +0100217 u32 status;
Laxman Dewangan85285472012-11-14 05:54:47 +0530218 unsigned int read_words = 0;
219 u8 *rx_buf = (u8 *)t->rx_buf + tsd->cur_rx_pos;
220
221 status = tegra_sflash_readl(tsd, SPI_STATUS);
222 while (!(status & SPI_RXF_EMPTY)) {
223 int i;
Michal Nazarewicza553a312013-12-08 16:35:11 +0100224 u32 x = tegra_sflash_readl(tsd, SPI_RX_FIFO);
Laxman Dewangan85285472012-11-14 05:54:47 +0530225 for (i = 0; (i < tsd->bytes_per_word); i++)
226 *rx_buf++ = (x >> (i*8)) & 0xFF;
227 read_words++;
228 status = tegra_sflash_readl(tsd, SPI_STATUS);
229 }
230 tsd->cur_rx_pos += read_words * tsd->bytes_per_word;
231 return 0;
232}
233
234static int tegra_sflash_start_cpu_based_transfer(
235 struct tegra_sflash_data *tsd, struct spi_transfer *t)
236{
Michal Nazarewicza553a312013-12-08 16:35:11 +0100237 u32 val = 0;
Laxman Dewangan85285472012-11-14 05:54:47 +0530238 unsigned cur_words;
239
240 if (tsd->cur_direction & DATA_DIR_TX)
241 val |= SPI_IE_TXC;
242
243 if (tsd->cur_direction & DATA_DIR_RX)
244 val |= SPI_IE_RXC;
245
246 tegra_sflash_writel(tsd, val, SPI_DMA_CTL);
247 tsd->dma_control_reg = val;
248
249 if (tsd->cur_direction & DATA_DIR_TX)
250 cur_words = tegra_sflash_fill_tx_fifo_from_client_txbuf(tsd, t);
251 else
252 cur_words = tsd->curr_xfer_words;
253 val |= SPI_DMA_BLK_COUNT(cur_words);
254 tegra_sflash_writel(tsd, val, SPI_DMA_CTL);
255 tsd->dma_control_reg = val;
256 val |= SPI_DMA_EN;
257 tegra_sflash_writel(tsd, val, SPI_DMA_CTL);
258 return 0;
259}
260
261static int tegra_sflash_start_transfer_one(struct spi_device *spi,
262 struct spi_transfer *t, bool is_first_of_msg,
263 bool is_single_xfer)
264{
265 struct tegra_sflash_data *tsd = spi_master_get_devdata(spi->master);
266 u32 speed;
Michal Nazarewicza553a312013-12-08 16:35:11 +0100267 u32 command;
Laxman Dewangan85285472012-11-14 05:54:47 +0530268
Laxman Dewanganbeb96c22013-01-05 00:17:15 +0530269 speed = t->speed_hz;
Laxman Dewangan85285472012-11-14 05:54:47 +0530270 if (speed != tsd->cur_speed) {
271 clk_set_rate(tsd->clk, speed);
272 tsd->cur_speed = speed;
273 }
274
275 tsd->cur_spi = spi;
276 tsd->cur_pos = 0;
277 tsd->cur_rx_pos = 0;
278 tsd->cur_tx_pos = 0;
279 tsd->curr_xfer = t;
280 tegra_sflash_calculate_curr_xfer_param(spi, tsd, t);
281 if (is_first_of_msg) {
282 command = tsd->def_command_reg;
283 command |= SPI_BIT_LENGTH(t->bits_per_word - 1);
284 command |= SPI_CS_VAL_HIGH;
285
286 command &= ~SPI_MODES;
287 if (spi->mode & SPI_CPHA)
288 command |= SPI_CK_SDA_FALLING;
289
290 if (spi->mode & SPI_CPOL)
291 command |= SPI_ACTIVE_SCLK_DRIVE_HIGH;
292 else
293 command |= SPI_ACTIVE_SCLK_DRIVE_LOW;
294 command |= SPI_CS0_EN << spi->chip_select;
295 } else {
296 command = tsd->command_reg;
297 command &= ~SPI_BIT_LENGTH(~0);
298 command |= SPI_BIT_LENGTH(t->bits_per_word - 1);
299 command &= ~(SPI_RX_EN | SPI_TX_EN);
300 }
301
302 tsd->cur_direction = 0;
303 if (t->rx_buf) {
304 command |= SPI_RX_EN;
305 tsd->cur_direction |= DATA_DIR_RX;
306 }
307 if (t->tx_buf) {
308 command |= SPI_TX_EN;
309 tsd->cur_direction |= DATA_DIR_TX;
310 }
311 tegra_sflash_writel(tsd, command, SPI_COMMAND);
312 tsd->command_reg = command;
313
Michal Nazarewicza553a312013-12-08 16:35:11 +0100314 return tegra_sflash_start_cpu_based_transfer(tsd, t);
Laxman Dewangan85285472012-11-14 05:54:47 +0530315}
316
Laxman Dewanganbeb96c22013-01-05 00:17:15 +0530317static int tegra_sflash_setup(struct spi_device *spi)
318{
319 struct tegra_sflash_data *tsd = spi_master_get_devdata(spi->master);
320
321 /* Set speed to the spi max fequency if spi device has not set */
322 spi->max_speed_hz = spi->max_speed_hz ? : tsd->spi_max_frequency;
323 return 0;
324}
325
Laxman Dewangan85285472012-11-14 05:54:47 +0530326static int tegra_sflash_transfer_one_message(struct spi_master *master,
327 struct spi_message *msg)
328{
329 bool is_first_msg = true;
330 int single_xfer;
331 struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
332 struct spi_transfer *xfer;
333 struct spi_device *spi = msg->spi;
334 int ret;
335
Laxman Dewangan85285472012-11-14 05:54:47 +0530336 msg->status = 0;
337 msg->actual_length = 0;
338 single_xfer = list_is_singular(&msg->transfers);
339 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800340 reinit_completion(&tsd->xfer_completion);
Laxman Dewangan85285472012-11-14 05:54:47 +0530341 ret = tegra_sflash_start_transfer_one(spi, xfer,
342 is_first_msg, single_xfer);
343 if (ret < 0) {
344 dev_err(tsd->dev,
345 "spi can not start transfer, err %d\n", ret);
346 goto exit;
347 }
348 is_first_msg = false;
349 ret = wait_for_completion_timeout(&tsd->xfer_completion,
350 SPI_DMA_TIMEOUT);
351 if (WARN_ON(ret == 0)) {
352 dev_err(tsd->dev,
353 "spi trasfer timeout, err %d\n", ret);
354 ret = -EIO;
355 goto exit;
356 }
357
358 if (tsd->tx_status || tsd->rx_status) {
359 dev_err(tsd->dev, "Error in Transfer\n");
360 ret = -EIO;
361 goto exit;
362 }
363 msg->actual_length += xfer->len;
364 if (xfer->cs_change && xfer->delay_usecs) {
365 tegra_sflash_writel(tsd, tsd->def_command_reg,
366 SPI_COMMAND);
367 udelay(xfer->delay_usecs);
368 }
369 }
370 ret = 0;
371exit:
372 tegra_sflash_writel(tsd, tsd->def_command_reg, SPI_COMMAND);
373 msg->status = ret;
374 spi_finalize_current_message(master);
Laxman Dewangan85285472012-11-14 05:54:47 +0530375 return ret;
376}
377
378static irqreturn_t handle_cpu_based_xfer(struct tegra_sflash_data *tsd)
379{
380 struct spi_transfer *t = tsd->curr_xfer;
381 unsigned long flags;
382
383 spin_lock_irqsave(&tsd->lock, flags);
384 if (tsd->tx_status || tsd->rx_status || (tsd->status_reg & SPI_BSY)) {
385 dev_err(tsd->dev,
386 "CpuXfer ERROR bit set 0x%x\n", tsd->status_reg);
387 dev_err(tsd->dev,
388 "CpuXfer 0x%08x:0x%08x\n", tsd->command_reg,
389 tsd->dma_control_reg);
390 tegra_periph_reset_assert(tsd->clk);
391 udelay(2);
392 tegra_periph_reset_deassert(tsd->clk);
393 complete(&tsd->xfer_completion);
394 goto exit;
395 }
396
397 if (tsd->cur_direction & DATA_DIR_RX)
398 tegra_sflash_read_rx_fifo_to_client_rxbuf(tsd, t);
399
400 if (tsd->cur_direction & DATA_DIR_TX)
401 tsd->cur_pos = tsd->cur_tx_pos;
402 else
403 tsd->cur_pos = tsd->cur_rx_pos;
404
405 if (tsd->cur_pos == t->len) {
406 complete(&tsd->xfer_completion);
407 goto exit;
408 }
409
410 tegra_sflash_calculate_curr_xfer_param(tsd->cur_spi, tsd, t);
411 tegra_sflash_start_cpu_based_transfer(tsd, t);
412exit:
413 spin_unlock_irqrestore(&tsd->lock, flags);
414 return IRQ_HANDLED;
415}
416
417static irqreturn_t tegra_sflash_isr(int irq, void *context_data)
418{
419 struct tegra_sflash_data *tsd = context_data;
420
421 tsd->status_reg = tegra_sflash_readl(tsd, SPI_STATUS);
422 if (tsd->cur_direction & DATA_DIR_TX)
423 tsd->tx_status = tsd->status_reg & SPI_TX_OVF;
424
425 if (tsd->cur_direction & DATA_DIR_RX)
426 tsd->rx_status = tsd->status_reg & SPI_RX_UNF;
427 tegra_sflash_clear_status(tsd);
428
429 return handle_cpu_based_xfer(tsd);
430}
431
Stephen Warrene25469592013-02-15 15:03:48 -0700432static void tegra_sflash_parse_dt(struct tegra_sflash_data *tsd)
Laxman Dewangan85285472012-11-14 05:54:47 +0530433{
Stephen Warrene25469592013-02-15 15:03:48 -0700434 struct device_node *np = tsd->dev->of_node;
Laxman Dewangan85285472012-11-14 05:54:47 +0530435
Stephen Warrene25469592013-02-15 15:03:48 -0700436 if (of_property_read_u32(np, "spi-max-frequency",
437 &tsd->spi_max_frequency))
438 tsd->spi_max_frequency = 25000000; /* 25MHz */
Laxman Dewangan85285472012-11-14 05:54:47 +0530439}
440
Grant Likelyfd4a3192012-12-07 16:57:14 +0000441static struct of_device_id tegra_sflash_of_match[] = {
Laxman Dewangan85285472012-11-14 05:54:47 +0530442 { .compatible = "nvidia,tegra20-sflash", },
443 {}
444};
445MODULE_DEVICE_TABLE(of, tegra_sflash_of_match);
446
Grant Likelyfd4a3192012-12-07 16:57:14 +0000447static int tegra_sflash_probe(struct platform_device *pdev)
Laxman Dewangan85285472012-11-14 05:54:47 +0530448{
449 struct spi_master *master;
450 struct tegra_sflash_data *tsd;
451 struct resource *r;
Laxman Dewangan85285472012-11-14 05:54:47 +0530452 int ret;
453 const struct of_device_id *match;
454
Stephen Warrene25469592013-02-15 15:03:48 -0700455 match = of_match_device(tegra_sflash_of_match, &pdev->dev);
Laxman Dewangan85285472012-11-14 05:54:47 +0530456 if (!match) {
457 dev_err(&pdev->dev, "Error: No device match found\n");
458 return -ENODEV;
459 }
460
Laxman Dewangan85285472012-11-14 05:54:47 +0530461 master = spi_alloc_master(&pdev->dev, sizeof(*tsd));
462 if (!master) {
463 dev_err(&pdev->dev, "master allocation failed\n");
464 return -ENOMEM;
465 }
466
467 /* the spi->mode bits understood by this driver: */
468 master->mode_bits = SPI_CPOL | SPI_CPHA;
Laxman Dewanganbeb96c22013-01-05 00:17:15 +0530469 master->setup = tegra_sflash_setup;
Laxman Dewangan85285472012-11-14 05:54:47 +0530470 master->transfer_one_message = tegra_sflash_transfer_one_message;
Mark Brown38315fd2013-07-28 15:37:46 +0100471 master->auto_runtime_pm = true;
Laxman Dewangan85285472012-11-14 05:54:47 +0530472 master->num_chipselect = MAX_CHIP_SELECT;
473 master->bus_num = -1;
474
Jingoo Han24b5a822013-05-23 19:20:40 +0900475 platform_set_drvdata(pdev, master);
Laxman Dewangan85285472012-11-14 05:54:47 +0530476 tsd = spi_master_get_devdata(master);
477 tsd->master = master;
478 tsd->dev = &pdev->dev;
479 spin_lock_init(&tsd->lock);
480
Stephen Warrene25469592013-02-15 15:03:48 -0700481 tegra_sflash_parse_dt(tsd);
482
Laxman Dewangan85285472012-11-14 05:54:47 +0530483 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb0ee5602013-01-21 11:09:18 +0100484 tsd->base = devm_ioremap_resource(&pdev->dev, r);
485 if (IS_ERR(tsd->base)) {
486 ret = PTR_ERR(tsd->base);
Laxman Dewangan85285472012-11-14 05:54:47 +0530487 goto exit_free_master;
488 }
489
490 tsd->irq = platform_get_irq(pdev, 0);
491 ret = request_irq(tsd->irq, tegra_sflash_isr, 0,
492 dev_name(&pdev->dev), tsd);
493 if (ret < 0) {
494 dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
495 tsd->irq);
496 goto exit_free_master;
497 }
498
Prashant Gaikwad3cb91902013-01-11 13:31:20 +0530499 tsd->clk = devm_clk_get(&pdev->dev, NULL);
Laxman Dewangan85285472012-11-14 05:54:47 +0530500 if (IS_ERR(tsd->clk)) {
501 dev_err(&pdev->dev, "can not get clock\n");
502 ret = PTR_ERR(tsd->clk);
503 goto exit_free_irq;
504 }
505
Laxman Dewangan85285472012-11-14 05:54:47 +0530506 init_completion(&tsd->xfer_completion);
507 pm_runtime_enable(&pdev->dev);
508 if (!pm_runtime_enabled(&pdev->dev)) {
509 ret = tegra_sflash_runtime_resume(&pdev->dev);
510 if (ret)
511 goto exit_pm_disable;
512 }
513
514 ret = pm_runtime_get_sync(&pdev->dev);
515 if (ret < 0) {
516 dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
517 goto exit_pm_disable;
518 }
519
520 /* Reset controller */
521 tegra_periph_reset_assert(tsd->clk);
522 udelay(2);
523 tegra_periph_reset_deassert(tsd->clk);
524
525 tsd->def_command_reg = SPI_M_S | SPI_CS_SW;
526 tegra_sflash_writel(tsd, tsd->def_command_reg, SPI_COMMAND);
527 pm_runtime_put(&pdev->dev);
528
529 master->dev.of_node = pdev->dev.of_node;
Jingoo Hanf12f7312013-09-24 13:50:25 +0900530 ret = devm_spi_register_master(&pdev->dev, master);
Laxman Dewangan85285472012-11-14 05:54:47 +0530531 if (ret < 0) {
532 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
533 goto exit_pm_disable;
534 }
535 return ret;
536
537exit_pm_disable:
538 pm_runtime_disable(&pdev->dev);
539 if (!pm_runtime_status_suspended(&pdev->dev))
540 tegra_sflash_runtime_suspend(&pdev->dev);
541exit_free_irq:
542 free_irq(tsd->irq, tsd);
543exit_free_master:
544 spi_master_put(master);
545 return ret;
546}
547
Grant Likelyfd4a3192012-12-07 16:57:14 +0000548static int tegra_sflash_remove(struct platform_device *pdev)
Laxman Dewangan85285472012-11-14 05:54:47 +0530549{
Jingoo Han24b5a822013-05-23 19:20:40 +0900550 struct spi_master *master = platform_get_drvdata(pdev);
Laxman Dewangan85285472012-11-14 05:54:47 +0530551 struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
552
553 free_irq(tsd->irq, tsd);
Laxman Dewangan85285472012-11-14 05:54:47 +0530554
555 pm_runtime_disable(&pdev->dev);
556 if (!pm_runtime_status_suspended(&pdev->dev))
557 tegra_sflash_runtime_suspend(&pdev->dev);
558
559 return 0;
560}
561
562#ifdef CONFIG_PM_SLEEP
563static int tegra_sflash_suspend(struct device *dev)
564{
565 struct spi_master *master = dev_get_drvdata(dev);
566
567 return spi_master_suspend(master);
568}
569
570static int tegra_sflash_resume(struct device *dev)
571{
572 struct spi_master *master = dev_get_drvdata(dev);
573 struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
574 int ret;
575
576 ret = pm_runtime_get_sync(dev);
577 if (ret < 0) {
578 dev_err(dev, "pm runtime failed, e = %d\n", ret);
579 return ret;
580 }
581 tegra_sflash_writel(tsd, tsd->command_reg, SPI_COMMAND);
582 pm_runtime_put(dev);
583
584 return spi_master_resume(master);
585}
586#endif
587
588static int tegra_sflash_runtime_suspend(struct device *dev)
589{
590 struct spi_master *master = dev_get_drvdata(dev);
591 struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
592
593 /* Flush all write which are in PPSB queue by reading back */
594 tegra_sflash_readl(tsd, SPI_COMMAND);
595
596 clk_disable_unprepare(tsd->clk);
597 return 0;
598}
599
600static int tegra_sflash_runtime_resume(struct device *dev)
601{
602 struct spi_master *master = dev_get_drvdata(dev);
603 struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
604 int ret;
605
606 ret = clk_prepare_enable(tsd->clk);
607 if (ret < 0) {
608 dev_err(tsd->dev, "clk_prepare failed: %d\n", ret);
609 return ret;
610 }
611 return 0;
612}
613
614static const struct dev_pm_ops slink_pm_ops = {
615 SET_RUNTIME_PM_OPS(tegra_sflash_runtime_suspend,
616 tegra_sflash_runtime_resume, NULL)
617 SET_SYSTEM_SLEEP_PM_OPS(tegra_sflash_suspend, tegra_sflash_resume)
618};
619static struct platform_driver tegra_sflash_driver = {
620 .driver = {
621 .name = "spi-tegra-sflash",
622 .owner = THIS_MODULE,
623 .pm = &slink_pm_ops,
Stephen Warrene25469592013-02-15 15:03:48 -0700624 .of_match_table = tegra_sflash_of_match,
Laxman Dewangan85285472012-11-14 05:54:47 +0530625 },
626 .probe = tegra_sflash_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000627 .remove = tegra_sflash_remove,
Laxman Dewangan85285472012-11-14 05:54:47 +0530628};
629module_platform_driver(tegra_sflash_driver);
630
631MODULE_ALIAS("platform:spi-tegra-sflash");
632MODULE_DESCRIPTION("NVIDIA Tegra20 Serial Flash Controller Driver");
633MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
634MODULE_LICENSE("GPL v2");