| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* drivers/net/ethernet/micrel/ks8851.c |
| 3 |
* |
| 4 |
* Copyright 2009 Simtec Electronics |
| 5 |
* http://www.simtec.co.uk/ |
| 6 |
* Ben Dooks <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 |
|
| 11 |
#include <linux/interrupt.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/kernel.h> |
| 14 |
#include <linux/netdevice.h> |
| 15 |
#include <linux/etherdevice.h> |
| 16 |
#include <linux/ethtool.h> |
| 17 |
#include <linux/iopoll.h> |
| 18 |
#include <linux/mii.h> |
| 19 |
|
| 20 |
#include <linux/platform_device.h> |
| 21 |
#include <linux/of_net.h> |
| 22 |
|
| 23 |
#include "ks8851.h" |
| 24 |
|
| 25 |
static int msg_enable; |
| 26 |
|
| 27 |
#define BE3 0x8000 /* Byte Enable 3 */ |
| 28 |
#define BE2 0x4000 /* Byte Enable 2 */ |
| 29 |
#define BE1 0x2000 /* Byte Enable 1 */ |
| 30 |
#define BE0 0x1000 /* Byte Enable 0 */ |
| 31 |
|
| 32 |
/** |
| 33 |
* struct ks8851_net_par - KS8851 Parallel driver private data |
| 34 |
* @ks8851: KS8851 driver common private data |
| 35 |
* @lock: Lock to ensure that the device is not accessed when busy. |
| 36 |
* @hw_addr : start address of data register. |
| 37 |
* @hw_addr_cmd : start address of command register. |
| 38 |
* @cmd_reg_cache : command register cached. |
| 39 |
* |
| 40 |
* The @lock ensures that the chip is protected when certain operations are |
| 41 |
* in progress. When the read or write packet transfer is in progress, most |
| 42 |
* of the chip registers are not accessible until the transfer is finished |
| 43 |
* and the DMA has been de-asserted. |
| 44 |
*/ |
| 45 |
struct ks8851_net_par { |
| 46 |
struct ks8851_net ks8851; |
| 47 |
spinlock_t lock; |
| 48 |
void __iomem *hw_addr; |
| 49 |
void __iomem *hw_addr_cmd; |
| 50 |
u16 cmd_reg_cache; |
| 51 |
}; |
| 52 |
|
| 53 |
#define to_ks8851_par(ks) container_of((ks), struct ks8851_net_par, ks8851) |
| 54 |
|
| 55 |
/** |
| 56 |
* ks8851_lock_par - register access lock |
| 57 |
* @ks: The chip state |
| 58 |
* |
| 59 |
* Claim chip register access lock |
| 60 |
*/ |
| 61 |
static void ks8851_lock_par(struct ks8851_net *ks) |
| 62 |
{ |
| 63 |
struct ks8851_net_par *ksp = to_ks8851_par(ks); |
| 64 |
|
| 65 |
spin_lock_bh(&ksp->lock); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* ks8851_unlock_par - register access unlock |
| 70 |
* @ks: The chip state |
| 71 |
* |
| 72 |
* Release chip register access lock |
| 73 |
*/ |
| 74 |
static void ks8851_unlock_par(struct ks8851_net *ks) |
| 75 |
{ |
| 76 |
struct ks8851_net_par *ksp = to_ks8851_par(ks); |
| 77 |
|
| 78 |
spin_unlock_bh(&ksp->lock); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* ks_check_endian - Check whether endianness of the bus is correct |
| 83 |
* @ks : The chip information |
| 84 |
* |
| 85 |
* The KS8851-16MLL EESK pin allows selecting the endianness of the 16bit |
| 86 |
* bus. To maintain optimum performance, the bus endianness should be set |
| 87 |
* such that it matches the endianness of the CPU. |
| 88 |
*/ |
| 89 |
static int ks_check_endian(struct ks8851_net *ks) |
| 90 |
{ |
| 91 |
struct ks8851_net_par *ksp = to_ks8851_par(ks); |
| 92 |
u16 cider; |
| 93 |
|
| 94 |
/* |
| 95 |
* Read CIDER register first, however read it the "wrong" way around. |
| 96 |
* If the endian strap on the KS8851-16MLL in incorrect and the chip |
| 97 |
* is operating in different endianness than the CPU, then the meaning |
| 98 |
* of BE[3:0] byte-enable bits is also swapped such that: |
| 99 |
* BE[3,2,1,0] becomes BE[1,0,3,2] |
| 100 |
* |
| 101 |
* Luckily for us, the byte-enable bits are the top four MSbits of |
| 102 |
* the address register and the CIDER register is at offset 0xc0. |
| 103 |
* Hence, by reading address 0xc0c0, which is not impacted by endian |
| 104 |
* swapping, we assert either BE[3:2] or BE[1:0] while reading the |
| 105 |
* CIDER register. |
| 106 |
* |
| 107 |
* If the bus configuration is correct, reading 0xc0c0 asserts |
| 108 |
* BE[3:2] and this read returns 0x0000, because to read register |
| 109 |
* with bottom two LSbits of address set to 0, BE[1:0] must be |
| 110 |
* asserted. |
| 111 |
* |
| 112 |
* If the bus configuration is NOT correct, reading 0xc0c0 asserts |
| 113 |
* BE[1:0] and this read returns non-zero 0x8872 value. |
| 114 |
*/ |
| 115 |
iowrite16(BE3 | BE2 | KS_CIDER, ksp->hw_addr_cmd); |
| 116 |
cider = ioread16(ksp->hw_addr); |
| 117 |
if (!cider) |
| 118 |
return 0; |
| 119 |
|
| 120 |
netdev_err(ks->netdev, "incorrect EESK endian strap setting\n"); |
| 121 |
|
| 122 |
return -EINVAL; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* ks8851_wrreg16_par - write 16bit register value to chip |
| 127 |
* @ks: The chip state |
| 128 |
* @reg: The register address |
| 129 |
* @val: The value to write |
| 130 |
* |
| 131 |
* Issue a write to put the value @val into the register specified in @reg. |
| 132 |
*/ |
| 133 |
static void ks8851_wrreg16_par(struct ks8851_net *ks, unsigned int reg, |
| 134 |
unsigned int val) |
| 135 |
{ |
| 136 |
struct ks8851_net_par *ksp = to_ks8851_par(ks); |
| 137 |
|
| 138 |
ksp->cmd_reg_cache = (u16)reg | ((BE1 | BE0) << (reg & 0x02)); |
| 139 |
iowrite16(ksp->cmd_reg_cache, ksp->hw_addr_cmd); |
| 140 |
iowrite16(val, ksp->hw_addr); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* ks8851_rdreg16_par - read 16 bit register from chip |
| 145 |
* @ks: The chip information |
| 146 |
* @reg: The register address |
| 147 |
* |
| 148 |
* Read a 16bit register from the chip, returning the result |
| 149 |
*/ |
| 150 |
static unsigned int ks8851_rdreg16_par(struct ks8851_net *ks, unsigned int reg) |
| 151 |
{ |
| 152 |
struct ks8851_net_par *ksp = to_ks8851_par(ks); |
| 153 |
|
| 154 |
ksp->cmd_reg_cache = (u16)reg | ((BE1 | BE0) << (reg & 0x02)); |
| 155 |
iowrite16(ksp->cmd_reg_cache, ksp->hw_addr_cmd); |
| 156 |
return ioread16(ksp->hw_addr); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* ks8851_rdfifo_par - read data from the receive fifo |
| 161 |
* @ks: The device state. |
| 162 |
* @buff: The buffer address |
| 163 |
* @len: The length of the data to read |
| 164 |
* |
| 165 |
* Issue an RXQ FIFO read command and read the @len amount of data from |
| 166 |
* the FIFO into the buffer specified by @buff. |
| 167 |
*/ |
| 168 |
static void ks8851_rdfifo_par(struct ks8851_net *ks, u8 *buff, unsigned int len) |
| 169 |
{ |
| 170 |
struct ks8851_net_par *ksp = to_ks8851_par(ks); |
| 171 |
|
| 172 |
netif_dbg(ks, rx_status, ks->netdev, |
| 173 |
"%s: %d@%p\n", __func__, len, buff); |
| 174 |
|
| 175 |
ioread16_rep(ksp->hw_addr, (u16 *)buff + 1, len / 2); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* ks8851_wrfifo_par - write packet to TX FIFO |
| 180 |
* @ks: The device state. |
| 181 |
* @txp: The sk_buff to transmit. |
| 182 |
* @irq: IRQ on completion of the packet. |
| 183 |
* |
| 184 |
* Send the @txp to the chip. This means creating the relevant packet header |
| 185 |
* specifying the length of the packet and the other information the chip |
| 186 |
* needs, such as IRQ on completion. Send the header and the packet data to |
| 187 |
* the device. |
| 188 |
*/ |
| 189 |
static void ks8851_wrfifo_par(struct ks8851_net *ks, struct sk_buff *txp, |
| 190 |
bool irq) |
| 191 |
{ |
| 192 |
struct ks8851_net_par *ksp = to_ks8851_par(ks); |
| 193 |
unsigned int len = ALIGN(txp->len, 4); |
| 194 |
unsigned int fid = 0; |
| 195 |
|
| 196 |
netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n", |
| 197 |
__func__, txp, txp->len, txp->data, irq); |
| 198 |
|
| 199 |
fid = ks->fid++; |
| 200 |
fid &= TXFR_TXFID_MASK; |
| 201 |
|
| 202 |
if (irq) |
| 203 |
fid |= TXFR_TXIC; /* irq on completion */ |
| 204 |
|
| 205 |
iowrite16(fid, ksp->hw_addr); |
| 206 |
iowrite16(txp->len, ksp->hw_addr); |
| 207 |
|
| 208 |
iowrite16_rep(ksp->hw_addr, txp->data, len / 2); |
| 209 |
} |
| 210 |
|
| 211 |
static unsigned int ks8851_rdreg16_par_txqcr(struct ks8851_net *ks) |
| 212 |
{ |
| 213 |
return ks8851_rdreg16_par(ks, KS_TXQCR); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* ks8851_start_xmit_par - transmit packet |
| 218 |
* @skb: The buffer to transmit |
| 219 |
* @dev: The device used to transmit the packet. |
| 220 |
* |
| 221 |
* Called by the network layer to transmit the @skb. Queue the packet for |
| 222 |
* the device and schedule the necessary work to transmit the packet when |
| 223 |
* it is free. |
| 224 |
* |
| 225 |
* We do this to firstly avoid sleeping with the network device locked, |
| 226 |
* and secondly so we can round up more than one packet to transmit which |
| 227 |
* means we can try and avoid generating too many transmit done interrupts. |
| 228 |
*/ |
| 229 |
static netdev_tx_t ks8851_start_xmit_par(struct sk_buff *skb, |
| 230 |
struct net_device *dev) |
| 231 |
{ |
| 232 |
struct ks8851_net *ks = netdev_priv(dev); |
| 233 |
netdev_tx_t ret = NETDEV_TX_OK; |
| 234 |
unsigned int txqcr; |
| 235 |
u16 txmir; |
| 236 |
int err; |
| 237 |
|
| 238 |
netif_dbg(ks, tx_queued, ks->netdev, |
| 239 |
"%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data); |
| 240 |
|
| 241 |
ks8851_lock_par(ks); |
| 242 |
|
| 243 |
txmir = ks8851_rdreg16_par(ks, KS_TXMIR) & 0x1fff; |
| 244 |
|
| 245 |
if (likely(txmir >= skb->len + 12)) { |
| 246 |
ks8851_wrreg16_par(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA); |
| 247 |
ks8851_wrfifo_par(ks, skb, false); |
| 248 |
ks8851_wrreg16_par(ks, KS_RXQCR, ks->rc_rxqcr); |
| 249 |
ks8851_wrreg16_par(ks, KS_TXQCR, TXQCR_METFE); |
| 250 |
|
| 251 |
err = readx_poll_timeout_atomic(ks8851_rdreg16_par_txqcr, ks, |
| 252 |
txqcr, !(txqcr & TXQCR_METFE), |
| 253 |
5, 1000000); |
| 254 |
if (err) |
| 255 |
ret = NETDEV_TX_BUSY; |
| 256 |
|
| 257 |
ks8851_done_tx(ks, skb); |
| 258 |
} else { |
| 259 |
ret = NETDEV_TX_BUSY; |
| 260 |
} |
| 261 |
|
| 262 |
ks8851_unlock_par(ks); |
| 263 |
|
| 264 |
return ret; |
| 265 |
} |
| 266 |
|
| 267 |
static int ks8851_probe_par(struct platform_device *pdev) |
| 268 |
{ |
| 269 |
struct device *dev = &pdev->dev; |
| 270 |
struct ks8851_net_par *ksp; |
| 271 |
struct net_device *netdev; |
| 272 |
struct ks8851_net *ks; |
| 273 |
int ret; |
| 274 |
|
| 275 |
netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_par)); |
| 276 |
if (!netdev) |
| 277 |
return -ENOMEM; |
| 278 |
|
| 279 |
ks = netdev_priv(netdev); |
| 280 |
|
| 281 |
ks->lock = ks8851_lock_par; |
| 282 |
ks->unlock = ks8851_unlock_par; |
| 283 |
ks->rdreg16 = ks8851_rdreg16_par; |
| 284 |
ks->wrreg16 = ks8851_wrreg16_par; |
| 285 |
ks->rdfifo = ks8851_rdfifo_par; |
| 286 |
ks->wrfifo = ks8851_wrfifo_par; |
| 287 |
ks->start_xmit = ks8851_start_xmit_par; |
| 288 |
|
| 289 |
#define STD_IRQ (IRQ_LCI | /* Link Change */ \ |
| 290 |
IRQ_RXI | /* RX done */ \ |
| 291 |
IRQ_RXPSI) /* RX process stop */ |
| 292 |
ks->rc_ier = STD_IRQ; |
| 293 |
|
| 294 |
ksp = to_ks8851_par(ks); |
| 295 |
spin_lock_init(&ksp->lock); |
| 296 |
|
| 297 |
ksp->hw_addr = devm_platform_ioremap_resource(pdev, 0); |
| 298 |
if (IS_ERR(ksp->hw_addr)) |
| 299 |
return PTR_ERR(ksp->hw_addr); |
| 300 |
|
| 301 |
ksp->hw_addr_cmd = devm_platform_ioremap_resource(pdev, 1); |
| 302 |
if (IS_ERR(ksp->hw_addr_cmd)) |
| 303 |
return PTR_ERR(ksp->hw_addr_cmd); |
| 304 |
|
| 305 |
ret = ks_check_endian(ks); |
| 306 |
if (ret) |
| 307 |
return ret; |
| 308 |
|
| 309 |
netdev->irq = platform_get_irq(pdev, 0); |
| 310 |
if (netdev->irq < 0) |
| 311 |
return netdev->irq; |
| 312 |
|
| 313 |
return ks8851_probe_common(netdev, dev, msg_enable); |
| 314 |
} |
| 315 |
|
| 316 |
static void ks8851_remove_par(struct platform_device *pdev) |
| 317 |
{ |
| 318 |
ks8851_remove_common(&pdev->dev); |
| 319 |
} |
| 320 |
|
| 321 |
static const struct of_device_id ks8851_match_table[] = { |
| 322 |
{ .compatible = "micrel,ks8851-mll" }, |
| 323 |
{ } |
| 324 |
}; |
| 325 |
MODULE_DEVICE_TABLE(of, ks8851_match_table); |
| 326 |
|
| 327 |
static struct platform_driver ks8851_driver = { |
| 328 |
.driver = { |
| 329 |
.name = "ks8851", |
| 330 |
.of_match_table = ks8851_match_table, |
| 331 |
.pm = &ks8851_pm_ops, |
| 332 |
}, |
| 333 |
.probe = ks8851_probe_par, |
| 334 |
.remove = ks8851_remove_par, |
| 335 |
}; |
| 336 |
module_platform_driver(ks8851_driver); |
| 337 |
|
| 338 |
MODULE_DESCRIPTION("KS8851 Network driver"); |
| 339 |
MODULE_AUTHOR("Ben Dooks <[email protected]>"); |
| 340 |
MODULE_LICENSE("GPL"); |
| 341 |
|
| 342 |
module_param_named(message, msg_enable, int, 0); |
| 343 |
MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)"); |