Lines Matching +full:spi +full:- +full:clk

1 // SPDX-License-Identifier: GPL-2.0
3 // spi-mt7621.c -- MediaTek MT7621 SPI controller driver
6 // Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
7 // Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>
9 // Some parts are based on spi-orion.c:
11 // Copyright (C) 2007-2008 Marvell Ltd.
13 #include <linux/clk.h>
19 #include <linux/spi/spi.h>
21 #define DRIVER_NAME "spi-mt7621"
61 static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi) in spidev_to_mt7621_spi() argument
63 return spi_controller_get_devdata(spi->master); in spidev_to_mt7621_spi()
68 return ioread32(rs->base + reg); in mt7621_spi_read()
73 iowrite32(val, rs->base + reg); in mt7621_spi_write()
76 static void mt7621_spi_set_cs(struct spi_device *spi, int enable) in mt7621_spi_set_cs() argument
78 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); in mt7621_spi_set_cs()
79 int cs = spi->chip_select; in mt7621_spi_set_cs()
84 * Select SPI device 7, enable "more buffer mode" and disable in mt7621_spi_set_cs()
85 * full-duplex (only half-duplex really works on this chip in mt7621_spi_set_cs()
93 rs->pending_write = 0; in mt7621_spi_set_cs()
100 static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed) in mt7621_spi_prepare() argument
102 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); in mt7621_spi_prepare()
106 dev_dbg(&spi->dev, "speed:%u\n", speed); in mt7621_spi_prepare()
108 rate = DIV_ROUND_UP(rs->sys_freq, speed); in mt7621_spi_prepare()
109 dev_dbg(&spi->dev, "rate-1:%u\n", rate); in mt7621_spi_prepare()
112 return -EINVAL; in mt7621_spi_prepare()
119 reg |= (rate - 2) << MASTER_RS_CLK_SEL_SHIFT; in mt7621_spi_prepare()
120 rs->speed = speed; in mt7621_spi_prepare()
123 if (spi->mode & SPI_LSB_FIRST) in mt7621_spi_prepare()
127 * This SPI controller seems to be tested on SPI flash only and some in mt7621_spi_prepare()
128 * bits are swizzled under other SPI modes probably due to incorrect in mt7621_spi_prepare()
152 return -ETIMEDOUT; in mt7621_spi_wait_till_ready()
161 * Combine with any pending write, and perform one or more half-duplex in mt7621_spi_read_half_duplex()
165 tx_len = rs->pending_write; in mt7621_spi_read_half_duplex()
166 rs->pending_write = 0; in mt7621_spi_read_half_duplex()
174 val |= (tx_len - 4) * 8; in mt7621_spi_read_half_duplex()
193 rx_len -= i; in mt7621_spi_read_half_duplex()
205 int len = rs->pending_write; in mt7621_spi_write_half_duplex()
211 val <<= (4 - len) * 8; in mt7621_spi_write_half_duplex()
218 rs->pending_write = len; in mt7621_spi_write_half_duplex()
227 /* The byte-order of the opcode is weird! */ in mt7621_spi_write_half_duplex()
229 mt7621_spi_write(rs, MT7621_SPI_OPCODE + len - 4, val); in mt7621_spi_write_half_duplex()
232 tx_len -= 1; in mt7621_spi_write_half_duplex()
238 val >>= (4 - len) * 8; in mt7621_spi_write_half_duplex()
243 rs->pending_write = len; in mt7621_spi_write_half_duplex()
250 struct spi_device *spi = m->spi; in mt7621_spi_transfer_one_message() local
251 unsigned int speed = spi->max_speed_hz; in mt7621_spi_transfer_one_message()
257 list_for_each_entry(t, &m->transfers, transfer_list) in mt7621_spi_transfer_one_message()
258 if (t->speed_hz < speed) in mt7621_spi_transfer_one_message()
259 speed = t->speed_hz; in mt7621_spi_transfer_one_message()
261 if (mt7621_spi_prepare(spi, speed)) { in mt7621_spi_transfer_one_message()
262 status = -EIO; in mt7621_spi_transfer_one_message()
267 mt7621_spi_set_cs(spi, 1); in mt7621_spi_transfer_one_message()
269 m->actual_length = 0; in mt7621_spi_transfer_one_message()
270 list_for_each_entry(t, &m->transfers, transfer_list) { in mt7621_spi_transfer_one_message()
271 if ((t->rx_buf) && (t->tx_buf)) { in mt7621_spi_transfer_one_message()
275 * (cmd_bit_cnt == 0). So the claimed full-duplex in mt7621_spi_transfer_one_message()
279 status = -EIO; in mt7621_spi_transfer_one_message()
281 } else if (t->rx_buf) { in mt7621_spi_transfer_one_message()
282 mt7621_spi_read_half_duplex(rs, t->len, t->rx_buf); in mt7621_spi_transfer_one_message()
283 } else if (t->tx_buf) { in mt7621_spi_transfer_one_message()
284 mt7621_spi_write_half_duplex(rs, t->len, t->tx_buf); in mt7621_spi_transfer_one_message()
286 m->actual_length += t->len; in mt7621_spi_transfer_one_message()
291 mt7621_spi_set_cs(spi, 0); in mt7621_spi_transfer_one_message()
294 m->status = status; in mt7621_spi_transfer_one_message()
300 static int mt7621_spi_setup(struct spi_device *spi) in mt7621_spi_setup() argument
302 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); in mt7621_spi_setup()
304 if ((spi->max_speed_hz == 0) || in mt7621_spi_setup()
305 (spi->max_speed_hz > (rs->sys_freq / 2))) in mt7621_spi_setup()
306 spi->max_speed_hz = rs->sys_freq / 2; in mt7621_spi_setup()
308 if (spi->max_speed_hz < (rs->sys_freq / 4097)) { in mt7621_spi_setup()
309 dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n", in mt7621_spi_setup()
310 spi->max_speed_hz); in mt7621_spi_setup()
311 return -EINVAL; in mt7621_spi_setup()
318 { .compatible = "ralink,mt7621-spi" },
329 struct clk *clk; in mt7621_spi_probe() local
332 match = of_match_device(mt7621_spi_match, &pdev->dev); in mt7621_spi_probe()
334 return -EINVAL; in mt7621_spi_probe()
340 clk = devm_clk_get_enabled(&pdev->dev, NULL); in mt7621_spi_probe()
341 if (IS_ERR(clk)) in mt7621_spi_probe()
342 return dev_err_probe(&pdev->dev, PTR_ERR(clk), in mt7621_spi_probe()
345 master = devm_spi_alloc_master(&pdev->dev, sizeof(*rs)); in mt7621_spi_probe()
347 dev_info(&pdev->dev, "master allocation failed\n"); in mt7621_spi_probe()
348 return -ENOMEM; in mt7621_spi_probe()
351 master->mode_bits = SPI_LSB_FIRST; in mt7621_spi_probe()
352 master->flags = SPI_CONTROLLER_HALF_DUPLEX; in mt7621_spi_probe()
353 master->setup = mt7621_spi_setup; in mt7621_spi_probe()
354 master->transfer_one_message = mt7621_spi_transfer_one_message; in mt7621_spi_probe()
355 master->bits_per_word_mask = SPI_BPW_MASK(8); in mt7621_spi_probe()
356 master->dev.of_node = pdev->dev.of_node; in mt7621_spi_probe()
357 master->num_chipselect = 2; in mt7621_spi_probe()
359 dev_set_drvdata(&pdev->dev, master); in mt7621_spi_probe()
362 rs->base = base; in mt7621_spi_probe()
363 rs->master = master; in mt7621_spi_probe()
364 rs->sys_freq = clk_get_rate(clk); in mt7621_spi_probe()
365 rs->pending_write = 0; in mt7621_spi_probe()
366 dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq); in mt7621_spi_probe()
368 ret = device_reset(&pdev->dev); in mt7621_spi_probe()
370 dev_err(&pdev->dev, "SPI reset failed!\n"); in mt7621_spi_probe()
374 return devm_spi_register_controller(&pdev->dev, master); in mt7621_spi_probe()
389 MODULE_DESCRIPTION("MT7621 SPI driver");