Initial commit

This commit is contained in:
domenico
2025-06-24 15:51:28 +02:00
commit 22031d9dab
6862 changed files with 1462554 additions and 0 deletions

View File

@@ -0,0 +1,220 @@
/*
* GPIO latch driver
*
* Copyright (C) 2014 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/platform_data/gpio-latch.h>
struct gpio_latch_chip {
struct gpio_chip gc;
struct mutex mutex;
struct mutex latch_mutex;
bool latch_enabled;
int le_gpio;
bool le_active_low;
int *gpios;
};
static inline struct gpio_latch_chip *to_gpio_latch_chip(struct gpio_chip *gc)
{
return container_of(gc, struct gpio_latch_chip, gc);
}
static void gpio_latch_lock(struct gpio_latch_chip *glc, bool enable)
{
mutex_lock(&glc->mutex);
if (enable)
glc->latch_enabled = true;
if (glc->latch_enabled)
mutex_lock(&glc->latch_mutex);
}
static void gpio_latch_unlock(struct gpio_latch_chip *glc, bool disable)
{
if (glc->latch_enabled)
mutex_unlock(&glc->latch_mutex);
if (disable)
glc->latch_enabled = true;
mutex_unlock(&glc->mutex);
}
static int
gpio_latch_get(struct gpio_chip *gc, unsigned offset)
{
struct gpio_latch_chip *glc = to_gpio_latch_chip(gc);
int ret;
gpio_latch_lock(glc, false);
ret = gpio_get_value(glc->gpios[offset]);
gpio_latch_unlock(glc, false);
return ret;
}
static void
gpio_latch_set(struct gpio_chip *gc, unsigned offset, int value)
{
struct gpio_latch_chip *glc = to_gpio_latch_chip(gc);
bool enable_latch = false;
bool disable_latch = false;
int gpio;
gpio = glc->gpios[offset];
if (gpio == glc->le_gpio) {
enable_latch = value ^ glc->le_active_low;
disable_latch = !enable_latch;
}
gpio_latch_lock(glc, enable_latch);
gpio_set_value(gpio, value);
gpio_latch_unlock(glc, disable_latch);
}
static int
gpio_latch_direction_input(struct gpio_chip *gc, unsigned offset)
{
struct gpio_latch_chip *glc = to_gpio_latch_chip(gc);
int ret;
gpio_latch_lock(glc, false);
ret = gpio_direction_input(glc->gpios[offset]);
gpio_latch_unlock(glc, false);
return ret;
}
static int
gpio_latch_direction_output(struct gpio_chip *gc, unsigned offset, int value)
{
struct gpio_latch_chip *glc = to_gpio_latch_chip(gc);
bool enable_latch = false;
bool disable_latch = false;
int gpio;
int ret;
gpio = glc->gpios[offset];
if (gpio == glc->le_gpio) {
enable_latch = value ^ glc->le_active_low;
disable_latch = !enable_latch;
}
gpio_latch_lock(glc, enable_latch);
ret = gpio_direction_output(gpio, value);
gpio_latch_unlock(glc, disable_latch);
return ret;
}
static int gpio_latch_probe(struct platform_device *pdev)
{
struct gpio_latch_chip *glc;
struct gpio_latch_platform_data *pdata;
struct gpio_chip *gc;
int size;
int ret;
int i;
pdata = dev_get_platdata(&pdev->dev);
if (!pdata)
return -EINVAL;
if (pdata->le_gpio_index >= pdata->num_gpios ||
!pdata->num_gpios ||
!pdata->gpios)
return -EINVAL;
for (i = 0; i < pdata->num_gpios; i++) {
int gpio = pdata->gpios[i];
ret = devm_gpio_request(&pdev->dev, gpio,
GPIO_LATCH_DRIVER_NAME);
if (ret)
return ret;
}
glc = devm_kzalloc(&pdev->dev, sizeof(*glc), GFP_KERNEL);
if (!glc)
return -ENOMEM;
mutex_init(&glc->mutex);
mutex_init(&glc->latch_mutex);
size = pdata->num_gpios * sizeof(glc->gpios[0]);
glc->gpios = devm_kzalloc(&pdev->dev, size , GFP_KERNEL);
if (!glc->gpios)
return -ENOMEM;
memcpy(glc->gpios, pdata->gpios, size);
glc->le_gpio = glc->gpios[pdata->le_gpio_index];
glc->le_active_low = pdata->le_active_low;
gc = &glc->gc;
gc->label = GPIO_LATCH_DRIVER_NAME;
gc->base = pdata->base;
gc->can_sleep = true;
gc->ngpio = pdata->num_gpios;
gc->get = gpio_latch_get;
gc->set = gpio_latch_set;
gc->direction_input = gpio_latch_direction_input,
gc->direction_output = gpio_latch_direction_output;
platform_set_drvdata(pdev, glc);
ret = gpiochip_add(&glc->gc);
if (ret)
return ret;
return 0;
}
static int gpio_latch_remove(struct platform_device *pdev)
{
struct gpio_latch_chip *glc = platform_get_drvdata(pdev);
gpiochip_remove(&glc->gc);
return 0;
}
static struct platform_driver gpio_latch_driver = {
.probe = gpio_latch_probe,
.remove = gpio_latch_remove,
.driver = {
.name = GPIO_LATCH_DRIVER_NAME,
.owner = THIS_MODULE,
},
};
static int __init gpio_latch_init(void)
{
return platform_driver_register(&gpio_latch_driver);
}
postcore_initcall(gpio_latch_init);
MODULE_DESCRIPTION("GPIO latch driver");
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" GPIO_LATCH_DRIVER_NAME);

View File

@@ -0,0 +1,243 @@
/*
* NXP 74HC153 - Dual 4-input multiplexer GPIO driver
*
* Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/nxp_74hc153.h>
#define NXP_74HC153_NUM_GPIOS 8
#define NXP_74HC153_S0_MASK 0x1
#define NXP_74HC153_S1_MASK 0x2
#define NXP_74HC153_BANK_MASK 0x4
struct nxp_74hc153_chip {
struct device *parent;
struct gpio_chip gpio_chip;
struct mutex lock;
};
static struct nxp_74hc153_chip *gpio_to_nxp(struct gpio_chip *gc)
{
return container_of(gc, struct nxp_74hc153_chip, gpio_chip);
}
static int nxp_74hc153_direction_input(struct gpio_chip *gc, unsigned offset)
{
return 0;
}
static int nxp_74hc153_direction_output(struct gpio_chip *gc,
unsigned offset, int val)
{
return -EINVAL;
}
static int nxp_74hc153_get_value(struct gpio_chip *gc, unsigned offset)
{
struct nxp_74hc153_chip *nxp;
struct nxp_74hc153_platform_data *pdata;
unsigned s0;
unsigned s1;
unsigned pin;
int ret;
nxp = gpio_to_nxp(gc);
pdata = nxp->parent->platform_data;
s0 = !!(offset & NXP_74HC153_S0_MASK);
s1 = !!(offset & NXP_74HC153_S1_MASK);
pin = (offset & NXP_74HC153_BANK_MASK) ? pdata->gpio_pin_2y
: pdata->gpio_pin_1y;
mutex_lock(&nxp->lock);
gpio_set_value(pdata->gpio_pin_s0, s0);
gpio_set_value(pdata->gpio_pin_s1, s1);
ret = gpio_get_value(pin);
mutex_unlock(&nxp->lock);
return ret;
}
static void nxp_74hc153_set_value(struct gpio_chip *gc,
unsigned offset, int val)
{
/* not supported */
}
static int nxp_74hc153_probe(struct platform_device *pdev)
{
struct nxp_74hc153_platform_data *pdata;
struct nxp_74hc153_chip *nxp;
struct gpio_chip *gc;
int err;
pdata = pdev->dev.platform_data;
if (pdata == NULL) {
dev_dbg(&pdev->dev, "no platform data specified\n");
return -EINVAL;
}
nxp = kzalloc(sizeof(struct nxp_74hc153_chip), GFP_KERNEL);
if (nxp == NULL) {
dev_err(&pdev->dev, "no memory for private data\n");
return -ENOMEM;
}
err = gpio_request(pdata->gpio_pin_s0, dev_name(&pdev->dev));
if (err) {
dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
pdata->gpio_pin_s0, err);
goto err_free_nxp;
}
err = gpio_request(pdata->gpio_pin_s1, dev_name(&pdev->dev));
if (err) {
dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
pdata->gpio_pin_s1, err);
goto err_free_s0;
}
err = gpio_request(pdata->gpio_pin_1y, dev_name(&pdev->dev));
if (err) {
dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
pdata->gpio_pin_1y, err);
goto err_free_s1;
}
err = gpio_request(pdata->gpio_pin_2y, dev_name(&pdev->dev));
if (err) {
dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
pdata->gpio_pin_2y, err);
goto err_free_1y;
}
err = gpio_direction_output(pdata->gpio_pin_s0, 0);
if (err) {
dev_err(&pdev->dev,
"unable to set direction of gpio %u, err=%d\n",
pdata->gpio_pin_s0, err);
goto err_free_2y;
}
err = gpio_direction_output(pdata->gpio_pin_s1, 0);
if (err) {
dev_err(&pdev->dev,
"unable to set direction of gpio %u, err=%d\n",
pdata->gpio_pin_s1, err);
goto err_free_2y;
}
err = gpio_direction_input(pdata->gpio_pin_1y);
if (err) {
dev_err(&pdev->dev,
"unable to set direction of gpio %u, err=%d\n",
pdata->gpio_pin_1y, err);
goto err_free_2y;
}
err = gpio_direction_input(pdata->gpio_pin_2y);
if (err) {
dev_err(&pdev->dev,
"unable to set direction of gpio %u, err=%d\n",
pdata->gpio_pin_2y, err);
goto err_free_2y;
}
nxp->parent = &pdev->dev;
mutex_init(&nxp->lock);
gc = &nxp->gpio_chip;
gc->direction_input = nxp_74hc153_direction_input;
gc->direction_output = nxp_74hc153_direction_output;
gc->get = nxp_74hc153_get_value;
gc->set = nxp_74hc153_set_value;
gc->can_sleep = 1;
gc->base = pdata->gpio_base;
gc->ngpio = NXP_74HC153_NUM_GPIOS;
gc->label = dev_name(nxp->parent);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
gc->dev = nxp->parent;
#else
gc->parent = nxp->parent;
#endif
gc->owner = THIS_MODULE;
err = gpiochip_add(&nxp->gpio_chip);
if (err) {
dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
goto err_free_2y;
}
platform_set_drvdata(pdev, nxp);
return 0;
err_free_2y:
gpio_free(pdata->gpio_pin_2y);
err_free_1y:
gpio_free(pdata->gpio_pin_1y);
err_free_s1:
gpio_free(pdata->gpio_pin_s1);
err_free_s0:
gpio_free(pdata->gpio_pin_s0);
err_free_nxp:
kfree(nxp);
return err;
}
static int nxp_74hc153_remove(struct platform_device *pdev)
{
struct nxp_74hc153_chip *nxp = platform_get_drvdata(pdev);
struct nxp_74hc153_platform_data *pdata = pdev->dev.platform_data;
if (nxp) {
gpiochip_remove(&nxp->gpio_chip);
gpio_free(pdata->gpio_pin_2y);
gpio_free(pdata->gpio_pin_1y);
gpio_free(pdata->gpio_pin_s1);
gpio_free(pdata->gpio_pin_s0);
kfree(nxp);
platform_set_drvdata(pdev, NULL);
}
return 0;
}
static struct platform_driver nxp_74hc153_driver = {
.probe = nxp_74hc153_probe,
.remove = nxp_74hc153_remove,
.driver = {
.name = NXP_74HC153_DRIVER_NAME,
.owner = THIS_MODULE,
},
};
static int __init nxp_74hc153_init(void)
{
return platform_driver_register(&nxp_74hc153_driver);
}
subsys_initcall(nxp_74hc153_init);
static void __exit nxp_74hc153_exit(void)
{
platform_driver_unregister(&nxp_74hc153_driver);
}
module_exit(nxp_74hc153_exit);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC153");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" NXP_74HC153_DRIVER_NAME);

View File

@@ -0,0 +1,396 @@
/*
* LED driver for NU801
*
* Kevin Paul Herbert
* Copyright (c) 2012, Meraki, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/leds-nu801.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#define MAX_NAME_LENGTH 24
#define NUM_COLORS 3
static const char * const led_nu801_colors[] = { "blue", "green", "red" };
struct led_nu801_led_data {
struct led_classdev cdev;
struct led_nu801_data *controller;
enum led_brightness level;
char name[MAX_NAME_LENGTH];
};
struct led_nu801_data {
unsigned cki;
unsigned sdi;
int lei;
struct delayed_work work;
struct led_nu801_led_data *led_chain;
int num_leds;
const char *device_name;
const char *name;
u32 ndelay;
atomic_t pending;
};
static void led_nu801_work(struct work_struct *work)
{
struct led_nu801_data *controller =
container_of(work, struct led_nu801_data, work.work);
struct led_nu801_led_data *led;
u16 bit;
u16 brightness;
int index;
for (index = 0; index < controller->num_leds; index++) {
led = &controller->led_chain[index];
brightness = led->level << 8; /* To do: gamma correction */
for (bit = 0x8000; bit; bit = bit >> 1) {
gpio_set_value(controller->sdi,
(brightness & bit) != 0);
gpio_set_value(controller->cki, 1);
if (unlikely(((index == (controller->num_leds - 1)) &&
(bit == 1) &&
(controller->lei < 0)))) {
udelay(600);
} else {
ndelay(controller->ndelay);
}
gpio_set_value(controller->cki, 0);
ndelay(controller->ndelay);
}
}
if (controller->lei >= 0) {
gpio_set_value(controller->lei, 1);
ndelay(controller->ndelay);
gpio_set_value(controller->lei, 0);
}
atomic_set(&controller->pending, 1);
}
static void led_nu801_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct led_nu801_led_data *led_dat =
container_of(led_cdev, struct led_nu801_led_data, cdev);
struct led_nu801_data *controller = led_dat->controller;
if (led_dat->level != value) {
led_dat->level = value;
if (atomic_dec_and_test(&controller->pending))
schedule_delayed_work(&led_dat->controller->work,
(HZ/1000) + 1);
}
}
static int led_nu801_create(struct led_nu801_data *controller,
struct device *parent,
int index,
enum led_brightness brightness,
#ifdef CONFIG_LEDS_TRIGGERS
const char *default_trigger,
#endif
const char *color)
{
struct led_nu801_led_data *led = &controller->led_chain[index];
int ret;
scnprintf(led->name, sizeof(led->name), "%s:%s:%s%d",
controller->device_name, color, controller->name,
(controller->num_leds - (index + 1)) / NUM_COLORS);
led->cdev.name = led->name;
led->cdev.brightness_set = led_nu801_set;
#ifdef CONFIG_LEDS_TRIGGERS
led->cdev.default_trigger = default_trigger;
#endif
led->level = brightness;
led->controller = controller;
ret = led_classdev_register(parent, &led->cdev);
if (ret < 0)
goto err;
return 0;
err:
kfree(led);
return ret;
}
static int
led_nu801_create_chain(const struct led_nu801_template *template,
struct led_nu801_data *controller,
struct device *parent)
{
int ret;
int index;
controller->cki = template->cki;
controller->sdi = template->sdi;
controller->lei = template->lei;
controller->num_leds = template->num_leds * 3;
controller->device_name = template->device_name;
controller->name = template->name;
controller->ndelay = template->ndelay;
atomic_set(&controller->pending, 1);
controller->led_chain = kzalloc(sizeof(struct led_nu801_led_data) *
controller->num_leds, GFP_KERNEL);
if (!controller->led_chain)
return -ENOMEM;
ret = gpio_request(controller->cki, template->name);
if (ret < 0)
goto err_free_chain;
ret = gpio_request(controller->sdi, template->name);
if (ret < 0)
goto err_ret_cki;
if (controller->lei >= 0) {
ret = gpio_request(controller->lei, template->name);
if (ret < 0)
goto err_ret_sdi;
ret = gpio_direction_output(controller->lei, 0);
if (ret < 0)
goto err_ret_lei;
}
ret = gpio_direction_output(controller->cki, 0);
if (ret < 0)
goto err_ret_lei;
ret = gpio_direction_output(controller->sdi, 0);
if (ret < 0)
goto err_ret_lei;
for (index = 0; index < controller->num_leds; index++) {
ret = led_nu801_create(controller, parent, index,
template->init_brightness
[index % NUM_COLORS],
#ifdef CONFIG_LEDS_TRIGGERS
template->default_trigger,
#endif
template->led_colors[index % NUM_COLORS] ?
template->led_colors[index % NUM_COLORS] :
led_nu801_colors[index % NUM_COLORS]);
if (ret < 0)
goto err_ret_sdi;
}
INIT_DELAYED_WORK(&controller->work, led_nu801_work);
schedule_delayed_work(&controller->work, 0);
return 0;
err_ret_lei:
if (controller->lei >= 0)
gpio_free(controller->lei);
err_ret_sdi:
gpio_free(controller->sdi);
err_ret_cki:
gpio_free(controller->cki);
err_free_chain:
kfree(controller->led_chain);
return ret;
}
static void led_nu801_delete_chain(struct led_nu801_data *controller)
{
struct led_nu801_led_data *led_chain;
struct led_nu801_led_data *led;
int index;
int num_leds;
led_chain = controller->led_chain;
controller->led_chain = 0;
num_leds = controller->num_leds;
controller->num_leds = 0;
cancel_delayed_work_sync(&controller->work);
for (index = 0; index < num_leds; index++) {
led = &led_chain[index];
led_classdev_unregister(&led->cdev);
}
gpio_free(controller->cki);
gpio_free(controller->sdi);
if (controller->lei >= 0)
gpio_free(controller->lei);
kfree(led_chain);
}
static struct led_nu801_data *
leds_nu801_create_of(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node, *child;
struct led_nu801_data *controllers;
int count = 0, ret;
int i = 0;
for_each_child_of_node(np, child)
count++;
if (!count)
return NULL;
controllers = kzalloc(sizeof(struct led_nu801_data) * count,
GFP_KERNEL);
if (!controllers)
return NULL;
for_each_child_of_node(np, child) {
const char *state;
struct led_nu801_template template = {};
struct device_node *colors;
int jj;
template.cki = of_get_named_gpio_flags(child, "cki", 0, NULL);
template.sdi = of_get_named_gpio_flags(child, "sdi", 0, NULL);
if (of_find_property(child, "lei", NULL)) {
template.lei = of_get_named_gpio_flags(child, "lei",
0, NULL);
} else {
template.lei = -1;
}
of_property_read_u32(child, "ndelay", &template.ndelay);
of_property_read_u32(child, "num_leds", &template.num_leds);
template.name = of_get_property(child, "label", NULL) ? :
child->name;
template.default_trigger = of_get_property(child,
"default-trigger", NULL);
jj = 0;
for_each_child_of_node(child, colors) {
template.led_colors[jj] = of_get_property(colors,
"label", NULL);
state = of_get_property(colors, "state", NULL);
if (!strncmp(state, "off", 3))
template.init_brightness[jj] = LED_OFF;
else if (!strncmp(state, "half", 4))
template.init_brightness[jj] = LED_HALF;
else if (!strncmp(state, "full", 4))
template.init_brightness[jj] = LED_FULL;
jj++;
}
ret = led_nu801_create_chain(&template,
&controllers[i],
&pdev->dev);
if (ret < 0)
goto err;
i++;
}
return controllers;
err:
for (i = i - 1; i >= 0; i--)
led_nu801_delete_chain(&controllers[i]);
kfree(controllers);
return NULL;
}
static int led_nu801_probe(struct platform_device *pdev)
{
struct led_nu801_platform_data *pdata = pdev->dev.platform_data;
struct led_nu801_data *controllers;
int i, ret = 0;
if (!(pdata && pdata->num_controllers)) {
controllers = leds_nu801_create_of(pdev);
if (!controllers)
return -ENODEV;
}
controllers = kzalloc(sizeof(struct led_nu801_data) *
pdata->num_controllers, GFP_KERNEL);
if (!controllers)
return -ENOMEM;
for (i = 0; i < pdata->num_controllers; i++) {
ret = led_nu801_create_chain(&pdata->template[i],
&controllers[i],
&pdev->dev);
if (ret < 0)
goto err;
}
platform_set_drvdata(pdev, controllers);
return 0;
err:
for (i = i - 1; i >= 0; i--)
led_nu801_delete_chain(&controllers[i]);
kfree(controllers);
return ret;
}
static int led_nu801_remove(struct platform_device *pdev)
{
int i;
struct led_nu801_platform_data *pdata = pdev->dev.platform_data;
struct led_nu801_data *controllers;
controllers = platform_get_drvdata(pdev);
for (i = 0; i < pdata->num_controllers; i++)
led_nu801_delete_chain(&controllers[i]);
kfree(controllers);
return 0;
}
static const struct of_device_id of_numen_leds_match[] = {
{ .compatible = "numen,leds-nu801", },
{},
};
MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
static struct platform_driver led_nu801_driver = {
.probe = led_nu801_probe,
.remove = led_nu801_remove,
.driver = {
.name = "leds-nu801",
.owner = THIS_MODULE,
.of_match_table = of_numen_leds_match,
},
};
static int __init led_nu801_init(void)
{
return platform_driver_register(&led_nu801_driver);
}
static void __exit led_nu801_exit(void)
{
platform_driver_unregister(&led_nu801_driver);
}
module_init(led_nu801_init);
module_exit(led_nu801_exit);
MODULE_AUTHOR("Kevin Paul Herbert <kph@meraki.net>");
MODULE_DESCRIPTION("NU801 LED driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:leds-nu801");

View File

@@ -0,0 +1,144 @@
/*
* LED driver for the RouterBOARD 750
*
* Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/slab.h>
#include <asm/mach-ath79/mach-rb750.h>
#define DRV_NAME "leds-rb750"
struct rb750_led_dev {
struct led_classdev cdev;
u32 mask;
int active_low;
void (*latch_change)(u32 clear, u32 set);
};
struct rb750_led_drvdata {
struct rb750_led_dev *led_devs;
int num_leds;
};
static inline struct rb750_led_dev *to_rbled(struct led_classdev *led_cdev)
{
return (struct rb750_led_dev *)container_of(led_cdev,
struct rb750_led_dev, cdev);
}
static void rb750_led_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct rb750_led_dev *rbled = to_rbled(led_cdev);
int level;
level = (value == LED_OFF) ? 0 : 1;
level ^= rbled->active_low;
if (level)
rbled->latch_change(0, rbled->mask);
else
rbled->latch_change(rbled->mask, 0);
}
static int rb750_led_probe(struct platform_device *pdev)
{
struct rb750_led_platform_data *pdata;
struct rb750_led_drvdata *drvdata;
int ret = 0;
int i;
pdata = pdev->dev.platform_data;
if (!pdata)
return -EINVAL;
drvdata = kzalloc(sizeof(struct rb750_led_drvdata) +
sizeof(struct rb750_led_dev) * pdata->num_leds,
GFP_KERNEL);
if (!drvdata)
return -ENOMEM;
drvdata->num_leds = pdata->num_leds;
drvdata->led_devs = (struct rb750_led_dev *) &drvdata[1];
for (i = 0; i < drvdata->num_leds; i++) {
struct rb750_led_dev *rbled = &drvdata->led_devs[i];
struct rb750_led_data *led_data = &pdata->leds[i];
rbled->cdev.name = led_data->name;
rbled->cdev.default_trigger = led_data->default_trigger;
rbled->cdev.brightness_set = rb750_led_brightness_set;
rbled->cdev.brightness = LED_OFF;
rbled->mask = led_data->mask;
rbled->active_low = !!led_data->active_low;
rbled->latch_change = pdata->latch_change;
ret = led_classdev_register(&pdev->dev, &rbled->cdev);
if (ret)
goto err;
}
platform_set_drvdata(pdev, drvdata);
return 0;
err:
for (i = i - 1; i >= 0; i--)
led_classdev_unregister(&drvdata->led_devs[i].cdev);
kfree(drvdata);
return ret;
}
static int rb750_led_remove(struct platform_device *pdev)
{
struct rb750_led_drvdata *drvdata;
int i;
drvdata = platform_get_drvdata(pdev);
for (i = 0; i < drvdata->num_leds; i++)
led_classdev_unregister(&drvdata->led_devs[i].cdev);
kfree(drvdata);
return 0;
}
static struct platform_driver rb750_led_driver = {
.probe = rb750_led_probe,
.remove = rb750_led_remove,
.driver = {
.name = DRV_NAME,
.owner = THIS_MODULE,
},
};
MODULE_ALIAS("platform:leds-rb750");
static int __init rb750_led_init(void)
{
return platform_driver_register(&rb750_led_driver);
}
static void __exit rb750_led_exit(void)
{
platform_driver_unregister(&rb750_led_driver);
}
module_init(rb750_led_init);
module_exit(rb750_led_exit);
MODULE_DESCRIPTION(DRV_NAME);
MODULE_DESCRIPTION("LED driver for the RouterBOARD 750");
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");

View File

@@ -0,0 +1,76 @@
/*
* USB LED driver for the NETGEAR WNDR3700
*
* Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <asm/mach-ath79/ar71xx_regs.h>
#include <asm/mach-ath79/ath79.h>
#define DRIVER_NAME "wndr3700-led-usb"
static void wndr3700_usb_led_set(struct led_classdev *cdev,
enum led_brightness brightness)
{
if (brightness)
ath79_device_reset_clear(AR71XX_RESET_GE1_PHY);
else
ath79_device_reset_set(AR71XX_RESET_GE1_PHY);
}
static enum led_brightness wndr3700_usb_led_get(struct led_classdev *cdev)
{
return ath79_device_reset_get(AR71XX_RESET_GE1_PHY) ? LED_OFF : LED_FULL;
}
static struct led_classdev wndr3700_usb_led = {
.name = "netgear:green:usb",
.brightness_set = wndr3700_usb_led_set,
.brightness_get = wndr3700_usb_led_get,
};
static int wndr3700_usb_led_probe(struct platform_device *pdev)
{
return led_classdev_register(&pdev->dev, &wndr3700_usb_led);
}
static int wndr3700_usb_led_remove(struct platform_device *pdev)
{
led_classdev_unregister(&wndr3700_usb_led);
return 0;
}
static struct platform_driver wndr3700_usb_led_driver = {
.probe = wndr3700_usb_led_probe,
.remove = wndr3700_usb_led_remove,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
},
};
static int __init wndr3700_usb_led_init(void)
{
return platform_driver_register(&wndr3700_usb_led_driver);
}
static void __exit wndr3700_usb_led_exit(void)
{
platform_driver_unregister(&wndr3700_usb_led_driver);
}
module_init(wndr3700_usb_led_init);
module_exit(wndr3700_usb_led_exit);
MODULE_DESCRIPTION("USB LED driver for the NETGEAR WNDR3700");
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DRIVER_NAME);

View File

@@ -0,0 +1,206 @@
/*
* Copyright (C) 2009 Christian Daniel <cd@maintech.de>
* Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* TRX flash partition table.
* Based on ar7 map by Felix Fietkau <nbd@nbd.name>
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/version.h>
struct cybertan_header {
char magic[4];
u8 res1[4];
char fw_date[3];
char fw_ver[3];
char id[4];
char hw_ver;
char unused;
u8 flags[2];
u8 res2[10];
};
#define TRX_PARTS 6
#define TRX_MAGIC 0x30524448
#define TRX_MAX_OFFSET 3
struct trx_header {
uint32_t magic; /* "HDR0" */
uint32_t len; /* Length of file including header */
uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
uint32_t flag_version; /* 0:15 flags, 16:31 version */
uint32_t offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
};
#define IH_MAGIC 0x27051956 /* Image Magic Number */
#define IH_NMLEN 32 /* Image Name Length */
struct uimage_header {
uint32_t ih_magic; /* Image Header Magic Number */
uint32_t ih_hcrc; /* Image Header CRC Checksum */
uint32_t ih_time; /* Image Creation Timestamp */
uint32_t ih_size; /* Image Data Size */
uint32_t ih_load; /* Data» Load Address */
uint32_t ih_ep; /* Entry Point Address */
uint32_t ih_dcrc; /* Image Data CRC Checksum */
uint8_t ih_os; /* Operating System */
uint8_t ih_arch; /* CPU architecture */
uint8_t ih_type; /* Image Type */
uint8_t ih_comp; /* Compression Type */
uint8_t ih_name[IH_NMLEN]; /* Image Name */
};
struct firmware_header {
struct cybertan_header cybertan;
struct trx_header trx;
struct uimage_header uimage;
} __packed;
#define UBOOT_LEN 0x40000
#define ART_LEN 0x10000
#define NVRAM_LEN 0x10000
static int cybertan_parse_partitions(struct mtd_info *master,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
struct mtd_partition **pparts,
#else
const struct mtd_partition **pparts,
#endif
struct mtd_part_parser_data *data)
{
struct firmware_header *header;
struct trx_header *theader;
struct uimage_header *uheader;
struct mtd_partition *trx_parts;
size_t retlen;
unsigned int kernel_len;
unsigned int uboot_len;
unsigned int nvram_len;
unsigned int art_len;
int ret;
uboot_len = max_t(unsigned int, master->erasesize, UBOOT_LEN);
nvram_len = max_t(unsigned int, master->erasesize, NVRAM_LEN);
art_len = max_t(unsigned int, master->erasesize, ART_LEN);
trx_parts = kzalloc(TRX_PARTS * sizeof(struct mtd_partition),
GFP_KERNEL);
if (!trx_parts) {
ret = -ENOMEM;
goto out;
}
header = vmalloc(sizeof(*header));
if (!header) {
return -ENOMEM;
goto free_parts;
}
ret = mtd_read(master, uboot_len, sizeof(*header),
&retlen, (void *) header);
if (ret)
goto free_hdr;
if (retlen != sizeof(*header)) {
ret = -EIO;
goto free_hdr;
}
theader = &header->trx;
if (le32_to_cpu(theader->magic) != TRX_MAGIC) {
printk(KERN_NOTICE "%s: no TRX header found\n", master->name);
goto free_hdr;
}
uheader = &header->uimage;
if (uheader->ih_magic != IH_MAGIC) {
printk(KERN_NOTICE "%s: no uImage found\n", master->name);
goto free_hdr;
}
kernel_len = le32_to_cpu(theader->offsets[1]) +
sizeof(struct cybertan_header);
trx_parts[0].name = "u-boot";
trx_parts[0].offset = 0;
trx_parts[0].size = uboot_len;
trx_parts[0].mask_flags = MTD_WRITEABLE;
trx_parts[1].name = "kernel";
trx_parts[1].offset = trx_parts[0].offset + trx_parts[0].size;
trx_parts[1].size = kernel_len;
trx_parts[1].mask_flags = 0;
trx_parts[2].name = "rootfs";
trx_parts[2].offset = trx_parts[1].offset + trx_parts[1].size;
trx_parts[2].size = master->size - uboot_len - nvram_len - art_len -
trx_parts[1].size;
trx_parts[2].mask_flags = 0;
trx_parts[3].name = "nvram";
trx_parts[3].offset = master->size - nvram_len - art_len;
trx_parts[3].size = nvram_len;
trx_parts[3].mask_flags = MTD_WRITEABLE;
trx_parts[4].name = "art";
trx_parts[4].offset = master->size - art_len;
trx_parts[4].size = art_len;
trx_parts[4].mask_flags = MTD_WRITEABLE;
trx_parts[5].name = "firmware";
trx_parts[5].offset = uboot_len;
trx_parts[5].size = master->size - uboot_len - nvram_len - art_len;
trx_parts[5].mask_flags = 0;
vfree(header);
*pparts = trx_parts;
return TRX_PARTS;
free_hdr:
vfree(header);
free_parts:
kfree(trx_parts);
out:
return ret;
}
static struct mtd_part_parser cybertan_parser = {
.owner = THIS_MODULE,
.parse_fn = cybertan_parse_partitions,
.name = "cybertan",
};
static int __init cybertan_parser_init(void)
{
register_mtd_parser(&cybertan_parser);
return 0;
}
module_init(cybertan_parser_init);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Christian Daniel <cd@maintech.de>");

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,392 @@
/*
* NAND flash driver for the MikroTik RouterBoard 4xx series
*
* Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
*
* This file was based on the driver for Linux 2.6.22 published by
* MikroTik for their RouterBoard 4xx series devices.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <asm/mach-ath79/ath79.h>
#include <asm/mach-ath79/rb4xx_cpld.h>
#define DRV_NAME "rb4xx-nand"
#define DRV_VERSION "0.2.0"
#define DRV_DESC "NAND flash driver for RouterBoard 4xx series"
#define RB4XX_NAND_GPIO_READY 5
#define RB4XX_NAND_GPIO_ALE 37
#define RB4XX_NAND_GPIO_CLE 38
#define RB4XX_NAND_GPIO_NCE 39
struct rb4xx_nand_info {
struct nand_chip chip;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
struct mtd_info mtd;
#endif
};
static inline struct rb4xx_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
return container_of(mtd, struct rb4xx_nand_info, mtd);
#else
struct nand_chip *chip = mtd_to_nand(mtd);
return container_of(chip, struct rb4xx_nand_info, chip);
#endif
}
static struct mtd_info *rbinfo_to_mtd(struct rb4xx_nand_info *nfc)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
return &nfc->mtd;
#else
return nand_to_mtd(&nfc->chip);
#endif
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
/*
* We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
* will not be able to find the kernel that we load.
*/
static struct nand_ecclayout rb4xx_nand_ecclayout = {
.eccbytes = 6,
.eccpos = { 8, 9, 10, 13, 14, 15 },
.oobavail = 9,
.oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
};
#else
static int rb4xx_ooblayout_ecc(struct mtd_info *mtd, int section,
struct mtd_oob_region *oobregion)
{
switch (section) {
case 0:
oobregion->offset = 8;
oobregion->length = 3;
return 0;
case 1:
oobregion->offset = 13;
oobregion->length = 3;
return 0;
default:
return -ERANGE;
}
}
static int rb4xx_ooblayout_free(struct mtd_info *mtd, int section,
struct mtd_oob_region *oobregion)
{
switch (section) {
case 0:
oobregion->offset = 0;
oobregion->length = 4;
return 0;
case 1:
oobregion->offset = 4;
oobregion->length = 1;
return 0;
case 2:
oobregion->offset = 6;
oobregion->length = 2;
return 0;
case 3:
oobregion->offset = 11;
oobregion->length = 2;
return 0;
default:
return -ERANGE;
}
}
static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops = {
.ecc = rb4xx_ooblayout_ecc,
.free = rb4xx_ooblayout_free,
};
#endif /* < 4.6 */
static struct mtd_partition rb4xx_nand_partitions[] = {
{
.name = "booter",
.offset = 0,
.size = (256 * 1024),
.mask_flags = MTD_WRITEABLE,
},
{
.name = "kernel",
.offset = (256 * 1024),
.size = (4 * 1024 * 1024) - (256 * 1024),
},
{
.name = "ubi",
.offset = MTDPART_OFS_NXTBLK,
.size = MTDPART_SIZ_FULL,
},
};
static int rb4xx_nand_dev_ready(struct mtd_info *mtd)
{
return gpio_get_value_cansleep(RB4XX_NAND_GPIO_READY);
}
static void rb4xx_nand_write_cmd(unsigned char cmd)
{
unsigned char data = cmd;
int err;
err = rb4xx_cpld_write(&data, 1);
if (err)
pr_err("rb4xx_nand: write cmd failed, err=%d\n", err);
}
static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
unsigned int ctrl)
{
if (ctrl & NAND_CTRL_CHANGE) {
gpio_set_value_cansleep(RB4XX_NAND_GPIO_CLE,
(ctrl & NAND_CLE) ? 1 : 0);
gpio_set_value_cansleep(RB4XX_NAND_GPIO_ALE,
(ctrl & NAND_ALE) ? 1 : 0);
gpio_set_value_cansleep(RB4XX_NAND_GPIO_NCE,
(ctrl & NAND_NCE) ? 0 : 1);
}
if (cmd != NAND_CMD_NONE)
rb4xx_nand_write_cmd(cmd);
}
static unsigned char rb4xx_nand_read_byte(struct mtd_info *mtd)
{
unsigned char data = 0;
int err;
err = rb4xx_cpld_read(&data, 1);
if (err) {
pr_err("rb4xx_nand: read data failed, err=%d\n", err);
data = 0xff;
}
return data;
}
static void rb4xx_nand_write_buf(struct mtd_info *mtd, const unsigned char *buf,
int len)
{
int err;
err = rb4xx_cpld_write(buf, len);
if (err)
pr_err("rb4xx_nand: write buf failed, err=%d\n", err);
}
static void rb4xx_nand_read_buf(struct mtd_info *mtd, unsigned char *buf,
int len)
{
int err;
err = rb4xx_cpld_read(buf, len);
if (err)
pr_err("rb4xx_nand: read buf failed, err=%d\n", err);
}
static int rb4xx_nand_probe(struct platform_device *pdev)
{
struct rb4xx_nand_info *info;
struct mtd_info *mtd;
int ret;
printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
ret = gpio_request(RB4XX_NAND_GPIO_READY, "NAND RDY");
if (ret) {
dev_err(&pdev->dev, "unable to request gpio %d\n",
RB4XX_NAND_GPIO_READY);
goto err;
}
ret = gpio_direction_input(RB4XX_NAND_GPIO_READY);
if (ret) {
dev_err(&pdev->dev, "unable to set input mode on gpio %d\n",
RB4XX_NAND_GPIO_READY);
goto err_free_gpio_ready;
}
ret = gpio_request(RB4XX_NAND_GPIO_ALE, "NAND ALE");
if (ret) {
dev_err(&pdev->dev, "unable to request gpio %d\n",
RB4XX_NAND_GPIO_ALE);
goto err_free_gpio_ready;
}
ret = gpio_direction_output(RB4XX_NAND_GPIO_ALE, 0);
if (ret) {
dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
RB4XX_NAND_GPIO_ALE);
goto err_free_gpio_ale;
}
ret = gpio_request(RB4XX_NAND_GPIO_CLE, "NAND CLE");
if (ret) {
dev_err(&pdev->dev, "unable to request gpio %d\n",
RB4XX_NAND_GPIO_CLE);
goto err_free_gpio_ale;
}
ret = gpio_direction_output(RB4XX_NAND_GPIO_CLE, 0);
if (ret) {
dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
RB4XX_NAND_GPIO_CLE);
goto err_free_gpio_cle;
}
ret = gpio_request(RB4XX_NAND_GPIO_NCE, "NAND NCE");
if (ret) {
dev_err(&pdev->dev, "unable to request gpio %d\n",
RB4XX_NAND_GPIO_NCE);
goto err_free_gpio_cle;
}
ret = gpio_direction_output(RB4XX_NAND_GPIO_NCE, 1);
if (ret) {
dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
RB4XX_NAND_GPIO_ALE);
goto err_free_gpio_nce;
}
info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info) {
dev_err(&pdev->dev, "rb4xx-nand: no memory for private data\n");
ret = -ENOMEM;
goto err_free_gpio_nce;
}
info->chip.priv = &info;
mtd = rbinfo_to_mtd(info);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
mtd->priv = &info->chip;
#endif
mtd->owner = THIS_MODULE;
info->chip.cmd_ctrl = rb4xx_nand_cmd_ctrl;
info->chip.dev_ready = rb4xx_nand_dev_ready;
info->chip.read_byte = rb4xx_nand_read_byte;
info->chip.write_buf = rb4xx_nand_write_buf;
info->chip.read_buf = rb4xx_nand_read_buf;
info->chip.chip_delay = 25;
info->chip.ecc.mode = NAND_ECC_SOFT;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
info->chip.ecc.algo = NAND_ECC_HAMMING;
#endif
info->chip.options = NAND_NO_SUBPAGE_WRITE;
platform_set_drvdata(pdev, info);
ret = nand_scan_ident(mtd, 1, NULL);
if (ret) {
ret = -ENXIO;
goto err_free_info;
}
if (mtd->writesize == 512)
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
info->chip.ecc.layout = &rb4xx_nand_ecclayout;
#else
mtd_set_ooblayout(mtd, &rb4xx_nand_ecclayout_ops);
#endif
ret = nand_scan_tail(mtd);
if (ret) {
return -ENXIO;
goto err_set_drvdata;
}
mtd_device_register(mtd, rb4xx_nand_partitions,
ARRAY_SIZE(rb4xx_nand_partitions));
if (ret)
goto err_release_nand;
return 0;
err_release_nand:
nand_release(&info->chip);
err_set_drvdata:
platform_set_drvdata(pdev, NULL);
err_free_info:
kfree(info);
err_free_gpio_nce:
gpio_free(RB4XX_NAND_GPIO_NCE);
err_free_gpio_cle:
gpio_free(RB4XX_NAND_GPIO_CLE);
err_free_gpio_ale:
gpio_free(RB4XX_NAND_GPIO_ALE);
err_free_gpio_ready:
gpio_free(RB4XX_NAND_GPIO_READY);
err:
return ret;
}
static int rb4xx_nand_remove(struct platform_device *pdev)
{
struct rb4xx_nand_info *info = platform_get_drvdata(pdev);
nand_release(&info->chip);
platform_set_drvdata(pdev, NULL);
kfree(info);
gpio_free(RB4XX_NAND_GPIO_NCE);
gpio_free(RB4XX_NAND_GPIO_CLE);
gpio_free(RB4XX_NAND_GPIO_ALE);
gpio_free(RB4XX_NAND_GPIO_READY);
return 0;
}
static struct platform_driver rb4xx_nand_driver = {
.probe = rb4xx_nand_probe,
.remove = rb4xx_nand_remove,
.driver = {
.name = DRV_NAME,
.owner = THIS_MODULE,
},
};
static int __init rb4xx_nand_init(void)
{
return platform_driver_register(&rb4xx_nand_driver);
}
static void __exit rb4xx_nand_exit(void)
{
platform_driver_unregister(&rb4xx_nand_driver);
}
module_init(rb4xx_nand_init);
module_exit(rb4xx_nand_exit);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
MODULE_LICENSE("GPL v2");

View File

@@ -0,0 +1,436 @@
/*
* NAND flash driver for the MikroTik RouterBOARD 750
*
* Copyright (C) 2010-2012 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <asm/mach-ath79/ar71xx_regs.h>
#include <asm/mach-ath79/ath79.h>
#include <asm/mach-ath79/mach-rb750.h>
#define DRV_NAME "rb750-nand"
#define DRV_VERSION "0.1.0"
#define DRV_DESC "NAND flash driver for the RouterBOARD 750"
#define RB750_NAND_IO0 BIT(RB750_GPIO_NAND_IO0)
#define RB750_NAND_ALE BIT(RB750_GPIO_NAND_ALE)
#define RB750_NAND_CLE BIT(RB750_GPIO_NAND_CLE)
#define RB750_NAND_NRE BIT(RB750_GPIO_NAND_NRE)
#define RB750_NAND_NWE BIT(RB750_GPIO_NAND_NWE)
#define RB750_NAND_RDY BIT(RB750_GPIO_NAND_RDY)
#define RB750_NAND_DATA_SHIFT 1
#define RB750_NAND_DATA_BITS (0xff << RB750_NAND_DATA_SHIFT)
#define RB750_NAND_INPUT_BITS (RB750_NAND_DATA_BITS | RB750_NAND_RDY)
#define RB750_NAND_OUTPUT_BITS (RB750_NAND_ALE | RB750_NAND_CLE | \
RB750_NAND_NRE | RB750_NAND_NWE)
struct rb750_nand_info {
struct nand_chip chip;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
struct mtd_info mtd;
#endif
struct rb7xx_nand_platform_data *pdata;
};
static inline struct rb750_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
return container_of(mtd, struct rb750_nand_info, mtd);
#else
struct nand_chip *chip = mtd_to_nand(mtd);
return container_of(chip, struct rb750_nand_info, chip);
#endif
}
static struct mtd_info *rbinfo_to_mtd(struct rb750_nand_info *nfc)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
return &nfc->mtd;
#else
return nand_to_mtd(&nfc->chip);
#endif
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
/*
* We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
* will not be able to find the kernel that we load.
*/
static struct nand_ecclayout rb750_nand_ecclayout = {
.eccbytes = 6,
.eccpos = { 8, 9, 10, 13, 14, 15 },
.oobavail = 9,
.oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
};
#else
static int rb750_ooblayout_ecc(struct mtd_info *mtd, int section,
struct mtd_oob_region *oobregion)
{
switch (section) {
case 0:
oobregion->offset = 8;
oobregion->length = 3;
return 0;
case 1:
oobregion->offset = 13;
oobregion->length = 3;
return 0;
default:
return -ERANGE;
}
}
static int rb750_ooblayout_free(struct mtd_info *mtd, int section,
struct mtd_oob_region *oobregion)
{
switch (section) {
case 0:
oobregion->offset = 0;
oobregion->length = 4;
return 0;
case 1:
oobregion->offset = 4;
oobregion->length = 1;
return 0;
case 2:
oobregion->offset = 6;
oobregion->length = 2;
return 0;
case 3:
oobregion->offset = 11;
oobregion->length = 2;
return 0;
default:
return -ERANGE;
}
}
static const struct mtd_ooblayout_ops rb750_nand_ecclayout_ops = {
.ecc = rb750_ooblayout_ecc,
.free = rb750_ooblayout_free,
};
#endif /* < 4.6 */
static struct mtd_partition rb750_nand_partitions[] = {
{
.name = "booter",
.offset = 0,
.size = (256 * 1024),
.mask_flags = MTD_WRITEABLE,
}, {
.name = "kernel",
.offset = (256 * 1024),
.size = (4 * 1024 * 1024) - (256 * 1024),
}, {
.name = "ubi",
.offset = MTDPART_OFS_NXTBLK,
.size = MTDPART_SIZ_FULL,
},
};
static void rb750_nand_write(const u8 *buf, unsigned len)
{
void __iomem *base = ath79_gpio_base;
u32 out;
u32 t;
unsigned i;
/* set data lines to output mode */
t = __raw_readl(base + AR71XX_GPIO_REG_OE);
__raw_writel(t | RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
out &= ~(RB750_NAND_DATA_BITS | RB750_NAND_NWE);
for (i = 0; i != len; i++) {
u32 data;
data = buf[i];
data <<= RB750_NAND_DATA_SHIFT;
data |= out;
__raw_writel(data, base + AR71XX_GPIO_REG_OUT);
__raw_writel(data | RB750_NAND_NWE, base + AR71XX_GPIO_REG_OUT);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_OUT);
}
/* set data lines to input mode */
t = __raw_readl(base + AR71XX_GPIO_REG_OE);
__raw_writel(t & ~RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_OE);
}
static void rb750_nand_read(u8 *read_buf, unsigned len)
{
void __iomem *base = ath79_gpio_base;
unsigned i;
for (i = 0; i < len; i++) {
u8 data;
/* activate RE line */
__raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_CLEAR);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_CLEAR);
/* read input lines */
data = __raw_readl(base + AR71XX_GPIO_REG_IN) >>
RB750_NAND_DATA_SHIFT;
/* deactivate RE line */
__raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_SET);
read_buf[i] = data;
}
}
static void rb750_nand_select_chip(struct mtd_info *mtd, int chip)
{
struct rb750_nand_info *rbinfo = mtd_to_rbinfo(mtd);
void __iomem *base = ath79_gpio_base;
u32 t;
if (chip >= 0) {
rbinfo->pdata->enable_pins();
/* set input mode for data lines */
t = __raw_readl(base + AR71XX_GPIO_REG_OE);
__raw_writel(t & ~RB750_NAND_INPUT_BITS,
base + AR71XX_GPIO_REG_OE);
/* deactivate RE and WE lines */
__raw_writel(RB750_NAND_NRE | RB750_NAND_NWE,
base + AR71XX_GPIO_REG_SET);
/* flush write */
(void) __raw_readl(base + AR71XX_GPIO_REG_SET);
/* activate CE line */
__raw_writel(rbinfo->pdata->nce_line,
base + AR71XX_GPIO_REG_CLEAR);
} else {
/* deactivate CE line */
__raw_writel(rbinfo->pdata->nce_line,
base + AR71XX_GPIO_REG_SET);
/* flush write */
(void) __raw_readl(base + AR71XX_GPIO_REG_SET);
t = __raw_readl(base + AR71XX_GPIO_REG_OE);
__raw_writel(t | RB750_NAND_IO0 | RB750_NAND_RDY,
base + AR71XX_GPIO_REG_OE);
rbinfo->pdata->disable_pins();
}
}
static int rb750_nand_dev_ready(struct mtd_info *mtd)
{
void __iomem *base = ath79_gpio_base;
return !!(__raw_readl(base + AR71XX_GPIO_REG_IN) & RB750_NAND_RDY);
}
static void rb750_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
unsigned int ctrl)
{
if (ctrl & NAND_CTRL_CHANGE) {
void __iomem *base = ath79_gpio_base;
u32 t;
t = __raw_readl(base + AR71XX_GPIO_REG_OUT);
t &= ~(RB750_NAND_CLE | RB750_NAND_ALE);
t |= (ctrl & NAND_CLE) ? RB750_NAND_CLE : 0;
t |= (ctrl & NAND_ALE) ? RB750_NAND_ALE : 0;
__raw_writel(t, base + AR71XX_GPIO_REG_OUT);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_OUT);
}
if (cmd != NAND_CMD_NONE) {
u8 t = cmd;
rb750_nand_write(&t, 1);
}
}
static u8 rb750_nand_read_byte(struct mtd_info *mtd)
{
u8 data = 0;
rb750_nand_read(&data, 1);
return data;
}
static void rb750_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
{
rb750_nand_read(buf, len);
}
static void rb750_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
{
rb750_nand_write(buf, len);
}
static void __init rb750_nand_gpio_init(struct rb750_nand_info *info)
{
void __iomem *base = ath79_gpio_base;
u32 out;
u32 t;
out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
/* setup output levels */
__raw_writel(RB750_NAND_NCE | RB750_NAND_NRE | RB750_NAND_NWE,
base + AR71XX_GPIO_REG_SET);
__raw_writel(RB750_NAND_ALE | RB750_NAND_CLE,
base + AR71XX_GPIO_REG_CLEAR);
/* setup input lines */
t = __raw_readl(base + AR71XX_GPIO_REG_OE);
__raw_writel(t & ~(RB750_NAND_INPUT_BITS), base + AR71XX_GPIO_REG_OE);
/* setup output lines */
t = __raw_readl(base + AR71XX_GPIO_REG_OE);
t |= RB750_NAND_OUTPUT_BITS;
t |= info->pdata->nce_line;
__raw_writel(t, base + AR71XX_GPIO_REG_OE);
info->pdata->latch_change(~out & RB750_NAND_IO0, out & RB750_NAND_IO0);
}
static int rb750_nand_probe(struct platform_device *pdev)
{
struct rb750_nand_info *info;
struct rb7xx_nand_platform_data *pdata;
struct mtd_info *mtd;
int ret;
printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
pdata = pdev->dev.platform_data;
if (!pdata)
return -EINVAL;
info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info)
return -ENOMEM;
info->chip.priv = &info;
mtd = rbinfo_to_mtd(info);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
mtd->priv = &info->chip;
#endif
mtd->owner = THIS_MODULE;
info->chip.select_chip = rb750_nand_select_chip;
info->chip.cmd_ctrl = rb750_nand_cmd_ctrl;
info->chip.dev_ready = rb750_nand_dev_ready;
info->chip.read_byte = rb750_nand_read_byte;
info->chip.write_buf = rb750_nand_write_buf;
info->chip.read_buf = rb750_nand_read_buf;
info->chip.chip_delay = 25;
info->chip.ecc.mode = NAND_ECC_SOFT;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
info->chip.ecc.algo = NAND_ECC_HAMMING;
#endif
info->chip.options = NAND_NO_SUBPAGE_WRITE;
info->pdata = pdata;
platform_set_drvdata(pdev, info);
rb750_nand_gpio_init(info);
ret = nand_scan_ident(mtd, 1, NULL);
if (ret) {
ret = -ENXIO;
goto err_free_info;
}
if (mtd->writesize == 512)
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
info->chip.ecc.layout = &rb750_nand_ecclayout;
#else
mtd_set_ooblayout(mtd, &rb750_nand_ecclayout_ops);
#endif
ret = nand_scan_tail(mtd);
if (ret) {
return -ENXIO;
goto err_set_drvdata;
}
ret = mtd_device_register(mtd, rb750_nand_partitions,
ARRAY_SIZE(rb750_nand_partitions));
if (ret)
goto err_release_nand;
return 0;
err_release_nand:
nand_release(&info->chip);
err_set_drvdata:
platform_set_drvdata(pdev, NULL);
err_free_info:
kfree(info);
return ret;
}
static int rb750_nand_remove(struct platform_device *pdev)
{
struct rb750_nand_info *info = platform_get_drvdata(pdev);
nand_release(&info->chip);
platform_set_drvdata(pdev, NULL);
kfree(info);
return 0;
}
static struct platform_driver rb750_nand_driver = {
.probe = rb750_nand_probe,
.remove = rb750_nand_remove,
.driver = {
.name = DRV_NAME,
.owner = THIS_MODULE,
},
};
static int __init rb750_nand_init(void)
{
return platform_driver_register(&rb750_nand_driver);
}
static void __exit rb750_nand_exit(void)
{
platform_driver_unregister(&rb750_nand_driver);
}
module_init(rb750_nand_init);
module_exit(rb750_nand_exit);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");

View File

@@ -0,0 +1,460 @@
/*
* NAND flash driver for the MikroTik RouterBOARD 91x series
*
* Copyright (C) 2013-2014 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/platform_data/rb91x_nand.h>
#include <linux/version.h>
#include <asm/mach-ath79/ar71xx_regs.h>
#include <asm/mach-ath79/ath79.h>
#define DRV_DESC "NAND flash driver for the RouterBOARD 91x series"
#define RB91X_NAND_NRWE BIT(12)
#define RB91X_NAND_DATA_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) |\
BIT(13) | BIT(14) | BIT(15))
#define RB91X_NAND_INPUT_BITS (RB91X_NAND_DATA_BITS | RB91X_NAND_RDY)
#define RB91X_NAND_OUTPUT_BITS (RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE)
#define RB91X_NAND_LOW_DATA_MASK 0x1f
#define RB91X_NAND_HIGH_DATA_MASK 0xe0
#define RB91X_NAND_HIGH_DATA_SHIFT 8
struct rb91x_nand_info {
struct nand_chip chip;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
struct mtd_info mtd;
#endif
struct device *dev;
int gpio_nce;
int gpio_ale;
int gpio_cle;
int gpio_rdy;
int gpio_read;
int gpio_nrw;
int gpio_nle;
};
static inline struct rb91x_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
return container_of(mtd, struct rb91x_nand_info, mtd);
#else
struct nand_chip *chip = mtd_to_nand(mtd);
return container_of(chip, struct rb91x_nand_info, chip);
#endif
}
static struct mtd_info *rbinfo_to_mtd(struct rb91x_nand_info *nfc)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
return &nfc->mtd;
#else
return nand_to_mtd(&nfc->chip);
#endif
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
/*
* We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
* will not be able to find the kernel that we load.
*/
static struct nand_ecclayout rb91x_nand_ecclayout = {
.eccbytes = 6,
.eccpos = { 8, 9, 10, 13, 14, 15 },
.oobavail = 9,
.oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
};
#else
static int rb91x_ooblayout_ecc(struct mtd_info *mtd, int section,
struct mtd_oob_region *oobregion)
{
switch (section) {
case 0:
oobregion->offset = 8;
oobregion->length = 3;
return 0;
case 1:
oobregion->offset = 13;
oobregion->length = 3;
return 0;
default:
return -ERANGE;
}
}
static int rb91x_ooblayout_free(struct mtd_info *mtd, int section,
struct mtd_oob_region *oobregion)
{
switch (section) {
case 0:
oobregion->offset = 0;
oobregion->length = 4;
return 0;
case 1:
oobregion->offset = 4;
oobregion->length = 1;
return 0;
case 2:
oobregion->offset = 6;
oobregion->length = 2;
return 0;
case 3:
oobregion->offset = 11;
oobregion->length = 2;
return 0;
default:
return -ERANGE;
}
}
static const struct mtd_ooblayout_ops rb91x_nand_ecclayout_ops = {
.ecc = rb91x_ooblayout_ecc,
.free = rb91x_ooblayout_free,
};
#endif /* < 4.6 */
static struct mtd_partition rb91x_nand_partitions[] = {
{
.name = "booter",
.offset = 0,
.size = (256 * 1024),
.mask_flags = MTD_WRITEABLE,
}, {
.name = "kernel",
.offset = (256 * 1024),
.size = (4 * 1024 * 1024) - (256 * 1024),
}, {
.name = "ubi",
.offset = MTDPART_OFS_NXTBLK,
.size = MTDPART_SIZ_FULL,
},
};
static void rb91x_nand_write(struct rb91x_nand_info *rbni,
const u8 *buf,
unsigned len)
{
void __iomem *base = ath79_gpio_base;
u32 oe_reg;
u32 out_reg;
u32 out;
unsigned i;
/* enable the latch */
gpio_set_value_cansleep(rbni->gpio_nle, 0);
oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE);
out_reg = __raw_readl(base + AR71XX_GPIO_REG_OUT);
/* set data lines to output mode */
__raw_writel(oe_reg & ~(RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE),
base + AR71XX_GPIO_REG_OE);
out = out_reg & ~(RB91X_NAND_DATA_BITS | RB91X_NAND_NRWE);
for (i = 0; i != len; i++) {
u32 data;
data = (buf[i] & RB91X_NAND_HIGH_DATA_MASK) <<
RB91X_NAND_HIGH_DATA_SHIFT;
data |= buf[i] & RB91X_NAND_LOW_DATA_MASK;
data |= out;
__raw_writel(data, base + AR71XX_GPIO_REG_OUT);
/* deactivate WE line */
data |= RB91X_NAND_NRWE;
__raw_writel(data, base + AR71XX_GPIO_REG_OUT);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_OUT);
}
/* restore registers */
__raw_writel(out_reg, base + AR71XX_GPIO_REG_OUT);
__raw_writel(oe_reg, base + AR71XX_GPIO_REG_OE);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_OUT);
/* disable the latch */
gpio_set_value_cansleep(rbni->gpio_nle, 1);
}
static void rb91x_nand_read(struct rb91x_nand_info *rbni,
u8 *read_buf,
unsigned len)
{
void __iomem *base = ath79_gpio_base;
u32 oe_reg;
u32 out_reg;
unsigned i;
/* enable read mode */
gpio_set_value_cansleep(rbni->gpio_read, 1);
/* enable latch */
gpio_set_value_cansleep(rbni->gpio_nle, 0);
/* save registers */
oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE);
out_reg = __raw_readl(base + AR71XX_GPIO_REG_OUT);
/* set data lines to input mode */
__raw_writel(oe_reg | RB91X_NAND_DATA_BITS,
base + AR71XX_GPIO_REG_OE);
for (i = 0; i < len; i++) {
u32 in;
u8 data;
/* activate RE line */
__raw_writel(RB91X_NAND_NRWE, base + AR71XX_GPIO_REG_CLEAR);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_CLEAR);
/* read input lines */
in = __raw_readl(base + AR71XX_GPIO_REG_IN);
/* deactivate RE line */
__raw_writel(RB91X_NAND_NRWE, base + AR71XX_GPIO_REG_SET);
data = (in & RB91X_NAND_LOW_DATA_MASK);
data |= (in >> RB91X_NAND_HIGH_DATA_SHIFT) &
RB91X_NAND_HIGH_DATA_MASK;
read_buf[i] = data;
}
/* restore registers */
__raw_writel(out_reg, base + AR71XX_GPIO_REG_OUT);
__raw_writel(oe_reg, base + AR71XX_GPIO_REG_OE);
/* flush write */
__raw_readl(base + AR71XX_GPIO_REG_OUT);
/* disable latch */
gpio_set_value_cansleep(rbni->gpio_nle, 1);
/* disable read mode */
gpio_set_value_cansleep(rbni->gpio_read, 0);
}
static int rb91x_nand_dev_ready(struct mtd_info *mtd)
{
struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
return gpio_get_value_cansleep(rbni->gpio_rdy);
}
static void rb91x_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
unsigned int ctrl)
{
struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
if (ctrl & NAND_CTRL_CHANGE) {
gpio_set_value_cansleep(rbni->gpio_cle,
(ctrl & NAND_CLE) ? 1 : 0);
gpio_set_value_cansleep(rbni->gpio_ale,
(ctrl & NAND_ALE) ? 1 : 0);
gpio_set_value_cansleep(rbni->gpio_nce,
(ctrl & NAND_NCE) ? 0 : 1);
}
if (cmd != NAND_CMD_NONE) {
u8 t = cmd;
rb91x_nand_write(rbni, &t, 1);
}
}
static u8 rb91x_nand_read_byte(struct mtd_info *mtd)
{
struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
u8 data = 0xff;
rb91x_nand_read(rbni, &data, 1);
return data;
}
static void rb91x_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
{
struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
rb91x_nand_read(rbni, buf, len);
}
static void rb91x_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
{
struct rb91x_nand_info *rbni = mtd_to_rbinfo(mtd);
rb91x_nand_write(rbni, buf, len);
}
static int rb91x_nand_gpio_init(struct rb91x_nand_info *info)
{
int ret;
/*
* Ensure that the LATCH is disabled before initializing
* control lines.
*/
ret = devm_gpio_request_one(info->dev, info->gpio_nle,
GPIOF_OUT_INIT_HIGH, "LATCH enable");
if (ret)
return ret;
ret = devm_gpio_request_one(info->dev, info->gpio_nce,
GPIOF_OUT_INIT_HIGH, "NAND nCE");
if (ret)
return ret;
ret = devm_gpio_request_one(info->dev, info->gpio_nrw,
GPIOF_OUT_INIT_HIGH, "NAND nRW");
if (ret)
return ret;
ret = devm_gpio_request_one(info->dev, info->gpio_cle,
GPIOF_OUT_INIT_LOW, "NAND CLE");
if (ret)
return ret;
ret = devm_gpio_request_one(info->dev, info->gpio_ale,
GPIOF_OUT_INIT_LOW, "NAND ALE");
if (ret)
return ret;
ret = devm_gpio_request_one(info->dev, info->gpio_read,
GPIOF_OUT_INIT_LOW, "NAND READ");
if (ret)
return ret;
ret = devm_gpio_request_one(info->dev, info->gpio_rdy,
GPIOF_IN, "NAND RDY");
return ret;
}
static int rb91x_nand_probe(struct platform_device *pdev)
{
struct rb91x_nand_info *rbni;
struct rb91x_nand_platform_data *pdata;
struct mtd_info *mtd;
int ret;
pr_info(DRV_DESC "\n");
pdata = dev_get_platdata(&pdev->dev);
if (!pdata)
return -EINVAL;
rbni = devm_kzalloc(&pdev->dev, sizeof(*rbni), GFP_KERNEL);
if (!rbni)
return -ENOMEM;
rbni->dev = &pdev->dev;
rbni->gpio_nce = pdata->gpio_nce;
rbni->gpio_ale = pdata->gpio_ale;
rbni->gpio_cle = pdata->gpio_cle;
rbni->gpio_read = pdata->gpio_read;
rbni->gpio_nrw = pdata->gpio_nrw;
rbni->gpio_rdy = pdata->gpio_rdy;
rbni->gpio_nle = pdata->gpio_nle;
rbni->chip.priv = &rbni;
mtd = rbinfo_to_mtd(rbni);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
mtd->priv = &rbni->chip;
#endif
mtd->owner = THIS_MODULE;
rbni->chip.cmd_ctrl = rb91x_nand_cmd_ctrl;
rbni->chip.dev_ready = rb91x_nand_dev_ready;
rbni->chip.read_byte = rb91x_nand_read_byte;
rbni->chip.write_buf = rb91x_nand_write_buf;
rbni->chip.read_buf = rb91x_nand_read_buf;
rbni->chip.chip_delay = 25;
rbni->chip.ecc.mode = NAND_ECC_SOFT;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
rbni->chip.ecc.algo = NAND_ECC_HAMMING;
#endif
rbni->chip.options = NAND_NO_SUBPAGE_WRITE;
platform_set_drvdata(pdev, rbni);
ret = rb91x_nand_gpio_init(rbni);
if (ret)
return ret;
ret = nand_scan_ident(mtd, 1, NULL);
if (ret)
return ret;
if (mtd->writesize == 512)
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
rbni->chip.ecc.layout = &rb91x_nand_ecclayout;
#else
mtd_set_ooblayout(mtd, &rb91x_nand_ecclayout_ops);
#endif
ret = nand_scan_tail(mtd);
if (ret)
return ret;
ret = mtd_device_register(mtd, rb91x_nand_partitions,
ARRAY_SIZE(rb91x_nand_partitions));
if (ret)
goto err_release_nand;
return 0;
err_release_nand:
nand_release(&rbni->chip);
return ret;
}
static int rb91x_nand_remove(struct platform_device *pdev)
{
struct rb91x_nand_info *info = platform_get_drvdata(pdev);
nand_release(&info->chip);
return 0;
}
static struct platform_driver rb91x_nand_driver = {
.probe = rb91x_nand_probe,
.remove = rb91x_nand_remove,
.driver = {
.name = RB91X_NAND_DRIVER_NAME,
.owner = THIS_MODULE,
},
};
module_platform_driver(rb91x_nand_driver);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");

View File

@@ -0,0 +1,235 @@
/*
* Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/magic.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/version.h>
#define TPLINK_NUM_PARTS 5
#define TPLINK_HEADER_V1 0x01000000
#define TPLINK_HEADER_V2 0x02000000
#define MD5SUM_LEN 16
#define TPLINK_ART_LEN 0x10000
#define TPLINK_KERNEL_OFFS 0x20000
#define TPLINK_64K_KERNEL_OFFS 0x10000
struct tplink_fw_header {
uint32_t version; /* header version */
char vendor_name[24];
char fw_version[36];
uint32_t hw_id; /* hardware id */
uint32_t hw_rev; /* hardware revision */
uint32_t unk1;
uint8_t md5sum1[MD5SUM_LEN];
uint32_t unk2;
uint8_t md5sum2[MD5SUM_LEN];
uint32_t unk3;
uint32_t kernel_la; /* kernel load address */
uint32_t kernel_ep; /* kernel entry point */
uint32_t fw_length; /* total length of the firmware */
uint32_t kernel_ofs; /* kernel data offset */
uint32_t kernel_len; /* kernel data length */
uint32_t rootfs_ofs; /* rootfs data offset */
uint32_t rootfs_len; /* rootfs data length */
uint32_t boot_ofs; /* bootloader data offset */
uint32_t boot_len; /* bootloader data length */
uint8_t pad[360];
} __attribute__ ((packed));
static struct tplink_fw_header *
tplink_read_header(struct mtd_info *mtd, size_t offset)
{
struct tplink_fw_header *header;
size_t header_len;
size_t retlen;
int ret;
u32 t;
header = vmalloc(sizeof(*header));
if (!header)
goto err;
header_len = sizeof(struct tplink_fw_header);
ret = mtd_read(mtd, offset, header_len, &retlen,
(unsigned char *) header);
if (ret)
goto err_free_header;
if (retlen != header_len)
goto err_free_header;
/* sanity checks */
t = be32_to_cpu(header->version);
if ((t != TPLINK_HEADER_V1) && (t != TPLINK_HEADER_V2))
goto err_free_header;
t = be32_to_cpu(header->kernel_ofs);
if (t != header_len)
goto err_free_header;
return header;
err_free_header:
vfree(header);
err:
return NULL;
}
static int tplink_check_rootfs_magic(struct mtd_info *mtd, size_t offset)
{
u32 magic;
size_t retlen;
int ret;
ret = mtd_read(mtd, offset, sizeof(magic), &retlen,
(unsigned char *) &magic);
if (ret)
return ret;
if (retlen != sizeof(magic))
return -EIO;
if (le32_to_cpu(magic) != SQUASHFS_MAGIC &&
magic != 0x19852003)
return -EINVAL;
return 0;
}
static int tplink_parse_partitions_offset(struct mtd_info *master,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
struct mtd_partition **pparts,
#else
const struct mtd_partition **pparts,
#endif
struct mtd_part_parser_data *data,
size_t offset)
{
struct mtd_partition *parts;
struct tplink_fw_header *header;
int nr_parts;
size_t art_offset;
size_t rootfs_offset;
size_t squashfs_offset;
int ret;
nr_parts = TPLINK_NUM_PARTS;
parts = kzalloc(nr_parts * sizeof(struct mtd_partition), GFP_KERNEL);
if (!parts) {
ret = -ENOMEM;
goto err;
}
header = tplink_read_header(master, offset);
if (!header) {
pr_notice("%s: no TP-Link header found\n", master->name);
ret = -ENODEV;
goto err_free_parts;
}
squashfs_offset = offset + sizeof(struct tplink_fw_header) +
be32_to_cpu(header->kernel_len);
ret = tplink_check_rootfs_magic(master, squashfs_offset);
if (ret == 0)
rootfs_offset = squashfs_offset;
else
rootfs_offset = offset + be32_to_cpu(header->rootfs_ofs);
art_offset = master->size - TPLINK_ART_LEN;
parts[0].name = "u-boot";
parts[0].offset = 0;
parts[0].size = offset;
parts[0].mask_flags = MTD_WRITEABLE;
parts[1].name = "kernel";
parts[1].offset = offset;
parts[1].size = rootfs_offset - offset;
parts[2].name = "rootfs";
parts[2].offset = rootfs_offset;
parts[2].size = art_offset - rootfs_offset;
parts[3].name = "art";
parts[3].offset = art_offset;
parts[3].size = TPLINK_ART_LEN;
parts[3].mask_flags = MTD_WRITEABLE;
parts[4].name = "firmware";
parts[4].offset = offset;
parts[4].size = art_offset - offset;
vfree(header);
*pparts = parts;
return nr_parts;
err_free_parts:
kfree(parts);
err:
*pparts = NULL;
return ret;
}
static int tplink_parse_partitions(struct mtd_info *master,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
struct mtd_partition **pparts,
#else
const struct mtd_partition **pparts,
#endif
struct mtd_part_parser_data *data)
{
return tplink_parse_partitions_offset(master, pparts, data,
TPLINK_KERNEL_OFFS);
}
static int tplink_parse_64k_partitions(struct mtd_info *master,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
struct mtd_partition **pparts,
#else
const struct mtd_partition **pparts,
#endif
struct mtd_part_parser_data *data)
{
return tplink_parse_partitions_offset(master, pparts, data,
TPLINK_64K_KERNEL_OFFS);
}
static struct mtd_part_parser tplink_parser = {
.owner = THIS_MODULE,
.parse_fn = tplink_parse_partitions,
.name = "tp-link",
};
static struct mtd_part_parser tplink_64k_parser = {
.owner = THIS_MODULE,
.parse_fn = tplink_parse_64k_partitions,
.name = "tp-link-64k",
};
static int __init tplink_parser_init(void)
{
register_mtd_parser(&tplink_parser);
register_mtd_parser(&tplink_64k_parser);
return 0;
}
module_init(tplink_parser_init);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");

View File

@@ -0,0 +1,289 @@
/*
* net/dsa/mv88e6063.c - Driver for Marvell 88e6063 switch chips
* Copyright (c) 2009 Gabor Juhos <juhosg@openwrt.org>
*
* This driver was base on: net/dsa/mv88e6060.c
* net/dsa/mv88e6063.c - Driver for Marvell 88e6060 switch chips
* Copyright (c) 2008-2009 Marvell Semiconductor
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/version.h>
#include <linux/list.h>
#include <linux/netdevice.h>
#include <linux/phy.h>
#include <net/dsa.h>
#include <linux/version.h>
#define REG_BASE 0x10
#define REG_PHY(p) (REG_BASE + (p))
#define REG_PORT(p) (REG_BASE + 8 + (p))
#define REG_GLOBAL (REG_BASE + 0x0f)
#define NUM_PORTS 7
static int reg_read(struct dsa_switch *ds, int addr, int reg)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
return mdiobus_read(bus, addr, reg);
#else
struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->dev);
return mdiobus_read(bus, addr, reg);
#endif
}
#define REG_READ(addr, reg) \
({ \
int __ret; \
\
__ret = reg_read(ds, addr, reg); \
if (__ret < 0) \
return __ret; \
__ret; \
})
static int reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
return mdiobus_write(bus, addr, reg, val);
#else
struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->dev);
return mdiobus_write(bus, addr, reg, val);
#endif
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
static enum dsa_tag_protocol mv88e6063_get_tag_protocol(struct dsa_switch *ds)
{
return DSA_TAG_PROTO_TRAILER;
}
#endif
#define REG_WRITE(addr, reg, val) \
({ \
int __ret; \
\
__ret = reg_write(ds, addr, reg, val); \
if (__ret < 0) \
return __ret; \
})
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
static char *mv88e6063_drv_probe(struct device *host_dev, int sw_addr)
#else
static const char *mv88e6063_drv_probe(struct device *dsa_dev,
struct device *host_dev, int sw_addr,
void **_priv)
#endif
{
struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
int ret;
if (!bus)
return NULL;
ret = mdiobus_read(bus, REG_PORT(0), 0x03);
if (ret >= 0) {
ret &= 0xfff0;
if (ret == 0x1530)
return "Marvell 88E6063";
}
return NULL;
}
static int mv88e6063_switch_reset(struct dsa_switch *ds)
{
int i;
int ret;
/*
* Set all ports to the disabled state.
*/
for (i = 0; i < NUM_PORTS; i++) {
ret = REG_READ(REG_PORT(i), 0x04);
REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
}
/*
* Wait for transmit queues to drain.
*/
msleep(2);
/*
* Reset the switch.
*/
REG_WRITE(REG_GLOBAL, 0x0a, 0xa130);
/*
* Wait up to one second for reset to complete.
*/
for (i = 0; i < 1000; i++) {
ret = REG_READ(REG_GLOBAL, 0x00);
if ((ret & 0x8000) == 0x0000)
break;
msleep(1);
}
if (i == 1000)
return -ETIMEDOUT;
return 0;
}
static int mv88e6063_setup_global(struct dsa_switch *ds)
{
/*
* Disable discarding of frames with excessive collisions,
* set the maximum frame size to 1536 bytes, and mask all
* interrupt sources.
*/
REG_WRITE(REG_GLOBAL, 0x04, 0x0800);
/*
* Enable automatic address learning, set the address
* database size to 1024 entries, and set the default aging
* time to 5 minutes.
*/
REG_WRITE(REG_GLOBAL, 0x0a, 0x2130);
return 0;
}
static int mv88e6063_setup_port(struct dsa_switch *ds, int p)
{
int addr = REG_PORT(p);
/*
* Do not force flow control, disable Ingress and Egress
* Header tagging, disable VLAN tunneling, and set the port
* state to Forwarding. Additionally, if this is the CPU
* port, enable Ingress and Egress Trailer tagging mode.
*/
REG_WRITE(addr, 0x04, dsa_is_cpu_port(ds, p) ? 0x4103 : 0x0003);
/*
* Port based VLAN map: give each port its own address
* database, allow the CPU port to talk to each of the 'real'
* ports, and allow each of the 'real' ports to only talk to
* the CPU port.
*/
REG_WRITE(addr, 0x06,
((p & 0xf) << 12) |
(dsa_is_cpu_port(ds, p) ?
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
ds->phys_port_mask :
#else
ds->enabled_port_mask :
#endif
(1 << ds->dst->cpu_port)));
/*
* Port Association Vector: when learning source addresses
* of packets, add the address to the address database using
* a port bitmap that has only the bit for this port set and
* the other bits clear.
*/
REG_WRITE(addr, 0x0b, 1 << p);
return 0;
}
static int mv88e6063_setup(struct dsa_switch *ds)
{
int i;
int ret;
ret = mv88e6063_switch_reset(ds);
if (ret < 0)
return ret;
/* @@@ initialise atu */
ret = mv88e6063_setup_global(ds);
if (ret < 0)
return ret;
for (i = 0; i < NUM_PORTS; i++) {
ret = mv88e6063_setup_port(ds, i);
if (ret < 0)
return ret;
}
return 0;
}
static int mv88e6063_set_addr(struct dsa_switch *ds, u8 *addr)
{
REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
return 0;
}
static int mv88e6063_port_to_phy_addr(int port)
{
if (port >= 0 && port <= NUM_PORTS)
return REG_PHY(port);
return -1;
}
static int mv88e6063_phy_read(struct dsa_switch *ds, int port, int regnum)
{
int addr;
addr = mv88e6063_port_to_phy_addr(port);
if (addr == -1)
return 0xffff;
return reg_read(ds, addr, regnum);
}
static int
mv88e6063_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
{
int addr;
addr = mv88e6063_port_to_phy_addr(port);
if (addr == -1)
return 0xffff;
return reg_write(ds, addr, regnum, val);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,9,0)
static struct dsa_switch_driver mv88e6063_switch_ops = {
#else
static struct dsa_switch_ops mv88e6063_switch_ops = {
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,8,0)
.tag_protocol = htons(ETH_P_TRAILER),
#else
.get_tag_protocol = mv88e6063_get_tag_protocol,
#endif
.probe = mv88e6063_drv_probe,
.setup = mv88e6063_setup,
.set_addr = mv88e6063_set_addr,
.phy_read = mv88e6063_phy_read,
.phy_write = mv88e6063_phy_write,
};
static int __init mv88e6063_init(void)
{
register_switch_driver(&mv88e6063_switch_ops);
return 0;
}
module_init(mv88e6063_init);
static void __exit mv88e6063_cleanup(void)
{
unregister_switch_driver(&mv88e6063_switch_ops);
}
module_exit(mv88e6063_cleanup);

View File

@@ -0,0 +1,33 @@
config AG71XX
tristate "Atheros AR7XXX/AR9XXX built-in ethernet mac support"
depends on ATH79
select PHYLIB
help
If you wish to compile a kernel for AR7XXX/91XXX and enable
ethernet support, then you should always answer Y to this.
if AG71XX
config AG71XX_DEBUG
bool "Atheros AR71xx built-in ethernet driver debugging"
default n
help
Atheros AR71xx built-in ethernet driver debugging messages.
config AG71XX_DEBUG_FS
bool "Atheros AR71xx built-in ethernet driver debugfs support"
depends on DEBUG_FS
default n
help
Say Y, if you need access to various statistics provided by
the ag71xx driver.
config AG71XX_AR8216_SUPPORT
bool "special support for the Atheros AR8216 switch"
default n
default y if ATH79_MACH_WNR2000 || ATH79_MACH_MZK_W04NU
help
Say 'y' here if you want to enable special support for the
Atheros AR8216 switch found on some boards.
endif

View File

@@ -0,0 +1,15 @@
#
# Makefile for the Atheros AR71xx built-in ethernet macs
#
ag71xx-y += ag71xx_main.o
ag71xx-y += ag71xx_ethtool.o
ag71xx-y += ag71xx_phy.o
ag71xx-y += ag71xx_mdio.o
ag71xx-y += ag71xx_ar7240.o
ag71xx-$(CONFIG_AG71XX_DEBUG_FS) += ag71xx_debugfs.o
ag71xx-$(CONFIG_AG71XX_AR8216_SUPPORT) += ag71xx_ar8216.o
obj-$(CONFIG_AG71XX) += ag71xx.o

View File

@@ -0,0 +1,508 @@
/*
* Atheros AR71xx built-in ethernet mac driver
*
* Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
*
* Based on Atheros' AG7100 driver
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#ifndef __AG71XX_H
#define __AG71XX_H
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/random.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/ethtool.h>
#include <linux/etherdevice.h>
#include <linux/if_vlan.h>
#include <linux/phy.h>
#include <linux/skbuff.h>
#include <linux/dma-mapping.h>
#include <linux/workqueue.h>
#include <linux/bitops.h>
#include <asm/mach-ath79/ar71xx_regs.h>
#include <asm/mach-ath79/ath79.h>
#include <asm/mach-ath79/ag71xx_platform.h>
#define AG71XX_DRV_NAME "ag71xx"
#define AG71XX_DRV_VERSION "0.5.35"
/*
* For our NAPI weight bigger does *NOT* mean better - it means more
* D-cache misses and lots more wasted cycles than we'll ever
* possibly gain from saving instructions.
*/
#define AG71XX_NAPI_WEIGHT 32
#define AG71XX_OOM_REFILL (1 + HZ/10)
#define AG71XX_INT_ERR (AG71XX_INT_RX_BE | AG71XX_INT_TX_BE)
#define AG71XX_INT_TX (AG71XX_INT_TX_PS)
#define AG71XX_INT_RX (AG71XX_INT_RX_PR | AG71XX_INT_RX_OF)
#define AG71XX_INT_POLL (AG71XX_INT_RX | AG71XX_INT_TX)
#define AG71XX_INT_INIT (AG71XX_INT_ERR | AG71XX_INT_POLL)
#define AG71XX_TX_MTU_LEN 1540
#define AG71XX_TX_RING_SPLIT 512
#define AG71XX_TX_RING_DS_PER_PKT DIV_ROUND_UP(AG71XX_TX_MTU_LEN, \
AG71XX_TX_RING_SPLIT)
#define AG71XX_TX_RING_SIZE_DEFAULT 128
#define AG71XX_RX_RING_SIZE_DEFAULT 256
#define AG71XX_TX_RING_SIZE_MAX 128
#define AG71XX_RX_RING_SIZE_MAX 256
#define QCA955X_SGMII_LINK_WAR_MAX_TRY 10
#ifdef CONFIG_AG71XX_DEBUG
#define DBG(fmt, args...) pr_debug(fmt, ## args)
#else
#define DBG(fmt, args...) do {} while (0)
#endif
#define ag71xx_assert(_cond) \
do { \
if (_cond) \
break; \
printk("%s,%d: assertion failed\n", __FILE__, __LINE__); \
BUG(); \
} while (0)
struct ag71xx_desc {
u32 data;
u32 ctrl;
#define DESC_EMPTY BIT(31)
#define DESC_MORE BIT(24)
#define DESC_PKTLEN_M 0xfff
u32 next;
u32 pad;
} __attribute__((aligned(4)));
#define AG71XX_DESC_SIZE roundup(sizeof(struct ag71xx_desc), \
L1_CACHE_BYTES)
struct ag71xx_buf {
union {
struct sk_buff *skb;
void *rx_buf;
};
union {
dma_addr_t dma_addr;
unsigned int len;
};
};
struct ag71xx_ring {
struct ag71xx_buf *buf;
u8 *descs_cpu;
dma_addr_t descs_dma;
u16 desc_split;
u16 order;
unsigned int curr;
unsigned int dirty;
};
struct ag71xx_mdio {
struct mii_bus *mii_bus;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
int mii_irq[PHY_MAX_ADDR];
#endif
void __iomem *mdio_base;
struct ag71xx_mdio_platform_data *pdata;
};
struct ag71xx_int_stats {
unsigned long rx_pr;
unsigned long rx_be;
unsigned long rx_of;
unsigned long tx_ps;
unsigned long tx_be;
unsigned long tx_ur;
unsigned long total;
};
struct ag71xx_napi_stats {
unsigned long napi_calls;
unsigned long rx_count;
unsigned long rx_packets;
unsigned long rx_packets_max;
unsigned long tx_count;
unsigned long tx_packets;
unsigned long tx_packets_max;
unsigned long rx[AG71XX_NAPI_WEIGHT + 1];
unsigned long tx[AG71XX_NAPI_WEIGHT + 1];
};
struct ag71xx_debug {
struct dentry *debugfs_dir;
struct ag71xx_int_stats int_stats;
struct ag71xx_napi_stats napi_stats;
};
struct ag71xx {
/*
* Critical data related to the per-packet data path are clustered
* early in this structure to help improve the D-cache footprint.
*/
struct ag71xx_ring rx_ring ____cacheline_aligned;
struct ag71xx_ring tx_ring ____cacheline_aligned;
unsigned int max_frame_len;
unsigned int desc_pktlen_mask;
unsigned int rx_buf_size;
struct net_device *dev;
struct platform_device *pdev;
spinlock_t lock;
struct napi_struct napi;
u32 msg_enable;
/*
* From this point onwards we're not looking at per-packet fields.
*/
void __iomem *mac_base;
struct ag71xx_desc *stop_desc;
dma_addr_t stop_desc_dma;
struct mii_bus *mii_bus;
struct phy_device *phy_dev;
void *phy_priv;
unsigned int link;
unsigned int speed;
int duplex;
struct delayed_work restart_work;
struct delayed_work link_work;
struct timer_list oom_timer;
#ifdef CONFIG_AG71XX_DEBUG_FS
struct ag71xx_debug debug;
#endif
};
extern struct ethtool_ops ag71xx_ethtool_ops;
void ag71xx_link_adjust(struct ag71xx *ag);
int ag71xx_mdio_driver_init(void) __init;
void ag71xx_mdio_driver_exit(void);
int ag71xx_phy_connect(struct ag71xx *ag);
void ag71xx_phy_disconnect(struct ag71xx *ag);
void ag71xx_phy_start(struct ag71xx *ag);
void ag71xx_phy_stop(struct ag71xx *ag);
static inline struct ag71xx_platform_data *ag71xx_get_pdata(struct ag71xx *ag)
{
return ag->pdev->dev.platform_data;
}
static inline int ag71xx_desc_empty(struct ag71xx_desc *desc)
{
return (desc->ctrl & DESC_EMPTY) != 0;
}
static inline struct ag71xx_desc *
ag71xx_ring_desc(struct ag71xx_ring *ring, int idx)
{
return (struct ag71xx_desc *) &ring->descs_cpu[idx * AG71XX_DESC_SIZE];
}
static inline int
ag71xx_ring_size_order(int size)
{
return fls(size - 1);
}
/* Register offsets */
#define AG71XX_REG_MAC_CFG1 0x0000
#define AG71XX_REG_MAC_CFG2 0x0004
#define AG71XX_REG_MAC_IPG 0x0008
#define AG71XX_REG_MAC_HDX 0x000c
#define AG71XX_REG_MAC_MFL 0x0010
#define AG71XX_REG_MII_CFG 0x0020
#define AG71XX_REG_MII_CMD 0x0024
#define AG71XX_REG_MII_ADDR 0x0028
#define AG71XX_REG_MII_CTRL 0x002c
#define AG71XX_REG_MII_STATUS 0x0030
#define AG71XX_REG_MII_IND 0x0034
#define AG71XX_REG_MAC_IFCTL 0x0038
#define AG71XX_REG_MAC_ADDR1 0x0040
#define AG71XX_REG_MAC_ADDR2 0x0044
#define AG71XX_REG_FIFO_CFG0 0x0048
#define AG71XX_REG_FIFO_CFG1 0x004c
#define AG71XX_REG_FIFO_CFG2 0x0050
#define AG71XX_REG_FIFO_CFG3 0x0054
#define AG71XX_REG_FIFO_CFG4 0x0058
#define AG71XX_REG_FIFO_CFG5 0x005c
#define AG71XX_REG_FIFO_RAM0 0x0060
#define AG71XX_REG_FIFO_RAM1 0x0064
#define AG71XX_REG_FIFO_RAM2 0x0068
#define AG71XX_REG_FIFO_RAM3 0x006c
#define AG71XX_REG_FIFO_RAM4 0x0070
#define AG71XX_REG_FIFO_RAM5 0x0074
#define AG71XX_REG_FIFO_RAM6 0x0078
#define AG71XX_REG_FIFO_RAM7 0x007c
#define AG71XX_REG_TX_CTRL 0x0180
#define AG71XX_REG_TX_DESC 0x0184
#define AG71XX_REG_TX_STATUS 0x0188
#define AG71XX_REG_RX_CTRL 0x018c
#define AG71XX_REG_RX_DESC 0x0190
#define AG71XX_REG_RX_STATUS 0x0194
#define AG71XX_REG_INT_ENABLE 0x0198
#define AG71XX_REG_INT_STATUS 0x019c
#define AG71XX_REG_FIFO_DEPTH 0x01a8
#define AG71XX_REG_RX_SM 0x01b0
#define AG71XX_REG_TX_SM 0x01b4
#define MAC_CFG1_TXE BIT(0) /* Tx Enable */
#define MAC_CFG1_STX BIT(1) /* Synchronize Tx Enable */
#define MAC_CFG1_RXE BIT(2) /* Rx Enable */
#define MAC_CFG1_SRX BIT(3) /* Synchronize Rx Enable */
#define MAC_CFG1_TFC BIT(4) /* Tx Flow Control Enable */
#define MAC_CFG1_RFC BIT(5) /* Rx Flow Control Enable */
#define MAC_CFG1_LB BIT(8) /* Loopback mode */
#define MAC_CFG1_SR BIT(31) /* Soft Reset */
#define MAC_CFG2_FDX BIT(0)
#define MAC_CFG2_CRC_EN BIT(1)
#define MAC_CFG2_PAD_CRC_EN BIT(2)
#define MAC_CFG2_LEN_CHECK BIT(4)
#define MAC_CFG2_HUGE_FRAME_EN BIT(5)
#define MAC_CFG2_IF_1000 BIT(9)
#define MAC_CFG2_IF_10_100 BIT(8)
#define FIFO_CFG0_WTM BIT(0) /* Watermark Module */
#define FIFO_CFG0_RXS BIT(1) /* Rx System Module */
#define FIFO_CFG0_RXF BIT(2) /* Rx Fabric Module */
#define FIFO_CFG0_TXS BIT(3) /* Tx System Module */
#define FIFO_CFG0_TXF BIT(4) /* Tx Fabric Module */
#define FIFO_CFG0_ALL (FIFO_CFG0_WTM | FIFO_CFG0_RXS | FIFO_CFG0_RXF \
| FIFO_CFG0_TXS | FIFO_CFG0_TXF)
#define FIFO_CFG0_ENABLE_SHIFT 8
#define FIFO_CFG4_DE BIT(0) /* Drop Event */
#define FIFO_CFG4_DV BIT(1) /* RX_DV Event */
#define FIFO_CFG4_FC BIT(2) /* False Carrier */
#define FIFO_CFG4_CE BIT(3) /* Code Error */
#define FIFO_CFG4_CR BIT(4) /* CRC error */
#define FIFO_CFG4_LM BIT(5) /* Length Mismatch */
#define FIFO_CFG4_LO BIT(6) /* Length out of range */
#define FIFO_CFG4_OK BIT(7) /* Packet is OK */
#define FIFO_CFG4_MC BIT(8) /* Multicast Packet */
#define FIFO_CFG4_BC BIT(9) /* Broadcast Packet */
#define FIFO_CFG4_DR BIT(10) /* Dribble */
#define FIFO_CFG4_LE BIT(11) /* Long Event */
#define FIFO_CFG4_CF BIT(12) /* Control Frame */
#define FIFO_CFG4_PF BIT(13) /* Pause Frame */
#define FIFO_CFG4_UO BIT(14) /* Unsupported Opcode */
#define FIFO_CFG4_VT BIT(15) /* VLAN tag detected */
#define FIFO_CFG4_FT BIT(16) /* Frame Truncated */
#define FIFO_CFG4_UC BIT(17) /* Unicast Packet */
#define FIFO_CFG5_DE BIT(0) /* Drop Event */
#define FIFO_CFG5_DV BIT(1) /* RX_DV Event */
#define FIFO_CFG5_FC BIT(2) /* False Carrier */
#define FIFO_CFG5_CE BIT(3) /* Code Error */
#define FIFO_CFG5_LM BIT(4) /* Length Mismatch */
#define FIFO_CFG5_LO BIT(5) /* Length Out of Range */
#define FIFO_CFG5_OK BIT(6) /* Packet is OK */
#define FIFO_CFG5_MC BIT(7) /* Multicast Packet */
#define FIFO_CFG5_BC BIT(8) /* Broadcast Packet */
#define FIFO_CFG5_DR BIT(9) /* Dribble */
#define FIFO_CFG5_CF BIT(10) /* Control Frame */
#define FIFO_CFG5_PF BIT(11) /* Pause Frame */
#define FIFO_CFG5_UO BIT(12) /* Unsupported Opcode */
#define FIFO_CFG5_VT BIT(13) /* VLAN tag detected */
#define FIFO_CFG5_LE BIT(14) /* Long Event */
#define FIFO_CFG5_FT BIT(15) /* Frame Truncated */
#define FIFO_CFG5_16 BIT(16) /* unknown */
#define FIFO_CFG5_17 BIT(17) /* unknown */
#define FIFO_CFG5_SF BIT(18) /* Short Frame */
#define FIFO_CFG5_BM BIT(19) /* Byte Mode */
#define AG71XX_INT_TX_PS BIT(0)
#define AG71XX_INT_TX_UR BIT(1)
#define AG71XX_INT_TX_BE BIT(3)
#define AG71XX_INT_RX_PR BIT(4)
#define AG71XX_INT_RX_OF BIT(6)
#define AG71XX_INT_RX_BE BIT(7)
#define MAC_IFCTL_SPEED BIT(16)
#define MII_CFG_CLK_DIV_4 0
#define MII_CFG_CLK_DIV_6 2
#define MII_CFG_CLK_DIV_8 3
#define MII_CFG_CLK_DIV_10 4
#define MII_CFG_CLK_DIV_14 5
#define MII_CFG_CLK_DIV_20 6
#define MII_CFG_CLK_DIV_28 7
#define MII_CFG_CLK_DIV_34 8
#define MII_CFG_CLK_DIV_42 9
#define MII_CFG_CLK_DIV_50 10
#define MII_CFG_CLK_DIV_58 11
#define MII_CFG_CLK_DIV_66 12
#define MII_CFG_CLK_DIV_74 13
#define MII_CFG_CLK_DIV_82 14
#define MII_CFG_CLK_DIV_98 15
#define MII_CFG_RESET BIT(31)
#define MII_CMD_WRITE 0x0
#define MII_CMD_READ 0x1
#define MII_ADDR_SHIFT 8
#define MII_IND_BUSY BIT(0)
#define MII_IND_INVALID BIT(2)
#define TX_CTRL_TXE BIT(0) /* Tx Enable */
#define TX_STATUS_PS BIT(0) /* Packet Sent */
#define TX_STATUS_UR BIT(1) /* Tx Underrun */
#define TX_STATUS_BE BIT(3) /* Bus Error */
#define RX_CTRL_RXE BIT(0) /* Rx Enable */
#define RX_STATUS_PR BIT(0) /* Packet Received */
#define RX_STATUS_OF BIT(2) /* Rx Overflow */
#define RX_STATUS_BE BIT(3) /* Bus Error */
static inline void ag71xx_check_reg_offset(struct ag71xx *ag, unsigned reg)
{
switch (reg) {
case AG71XX_REG_MAC_CFG1 ... AG71XX_REG_MAC_MFL:
case AG71XX_REG_MAC_IFCTL ... AG71XX_REG_TX_SM:
case AG71XX_REG_MII_CFG:
break;
default:
BUG();
}
}
static inline void ag71xx_wr(struct ag71xx *ag, unsigned reg, u32 value)
{
ag71xx_check_reg_offset(ag, reg);
__raw_writel(value, ag->mac_base + reg);
/* flush write */
(void) __raw_readl(ag->mac_base + reg);
}
static inline u32 ag71xx_rr(struct ag71xx *ag, unsigned reg)
{
ag71xx_check_reg_offset(ag, reg);
return __raw_readl(ag->mac_base + reg);
}
static inline void ag71xx_sb(struct ag71xx *ag, unsigned reg, u32 mask)
{
void __iomem *r;
ag71xx_check_reg_offset(ag, reg);
r = ag->mac_base + reg;
__raw_writel(__raw_readl(r) | mask, r);
/* flush write */
(void)__raw_readl(r);
}
static inline void ag71xx_cb(struct ag71xx *ag, unsigned reg, u32 mask)
{
void __iomem *r;
ag71xx_check_reg_offset(ag, reg);
r = ag->mac_base + reg;
__raw_writel(__raw_readl(r) & ~mask, r);
/* flush write */
(void) __raw_readl(r);
}
static inline void ag71xx_int_enable(struct ag71xx *ag, u32 ints)
{
ag71xx_sb(ag, AG71XX_REG_INT_ENABLE, ints);
}
static inline void ag71xx_int_disable(struct ag71xx *ag, u32 ints)
{
ag71xx_cb(ag, AG71XX_REG_INT_ENABLE, ints);
}
#ifdef CONFIG_AG71XX_AR8216_SUPPORT
void ag71xx_add_ar8216_header(struct ag71xx *ag, struct sk_buff *skb);
int ag71xx_remove_ar8216_header(struct ag71xx *ag, struct sk_buff *skb,
int pktlen);
static inline int ag71xx_has_ar8216(struct ag71xx *ag)
{
return ag71xx_get_pdata(ag)->has_ar8216;
}
#else
static inline void ag71xx_add_ar8216_header(struct ag71xx *ag,
struct sk_buff *skb)
{
}
static inline int ag71xx_remove_ar8216_header(struct ag71xx *ag,
struct sk_buff *skb,
int pktlen)
{
return 0;
}
static inline int ag71xx_has_ar8216(struct ag71xx *ag)
{
return 0;
}
#endif
#ifdef CONFIG_AG71XX_DEBUG_FS
int ag71xx_debugfs_root_init(void);
void ag71xx_debugfs_root_exit(void);
int ag71xx_debugfs_init(struct ag71xx *ag);
void ag71xx_debugfs_exit(struct ag71xx *ag);
void ag71xx_debugfs_update_int_stats(struct ag71xx *ag, u32 status);
void ag71xx_debugfs_update_napi_stats(struct ag71xx *ag, int rx, int tx);
#else
static inline int ag71xx_debugfs_root_init(void) { return 0; }
static inline void ag71xx_debugfs_root_exit(void) {}
static inline int ag71xx_debugfs_init(struct ag71xx *ag) { return 0; }
static inline void ag71xx_debugfs_exit(struct ag71xx *ag) {}
static inline void ag71xx_debugfs_update_int_stats(struct ag71xx *ag,
u32 status) {}
static inline void ag71xx_debugfs_update_napi_stats(struct ag71xx *ag,
int rx, int tx) {}
#endif /* CONFIG_AG71XX_DEBUG_FS */
void ag71xx_ar7240_start(struct ag71xx *ag);
void ag71xx_ar7240_stop(struct ag71xx *ag);
int ag71xx_ar7240_init(struct ag71xx *ag);
void ag71xx_ar7240_cleanup(struct ag71xx *ag);
int ag71xx_mdio_mii_read(struct ag71xx_mdio *am, int addr, int reg);
void ag71xx_mdio_mii_write(struct ag71xx_mdio *am, int addr, int reg, u16 val);
u16 ar7240sw_phy_read(struct mii_bus *mii, unsigned phy_addr,
unsigned reg_addr);
int ar7240sw_phy_write(struct mii_bus *mii, unsigned phy_addr,
unsigned reg_addr, u16 reg_val);
#endif /* _AG71XX_H */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,44 @@
/*
* Atheros AR71xx built-in ethernet mac driver
* Special support for the Atheros ar8216 switch chip
*
* Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
*
* Based on Atheros' AG7100 driver
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "ag71xx.h"
#define AR8216_PACKET_TYPE_MASK 0xf
#define AR8216_PACKET_TYPE_NORMAL 0
#define AR8216_HEADER_LEN 2
void ag71xx_add_ar8216_header(struct ag71xx *ag, struct sk_buff *skb)
{
skb_push(skb, AR8216_HEADER_LEN);
skb->data[0] = 0x10;
skb->data[1] = 0x80;
}
int ag71xx_remove_ar8216_header(struct ag71xx *ag, struct sk_buff *skb,
int pktlen)
{
u8 type;
type = skb->data[1] & AR8216_PACKET_TYPE_MASK;
switch (type) {
case AR8216_PACKET_TYPE_NORMAL:
break;
default:
return -EINVAL;
}
skb_pull(skb, AR8216_HEADER_LEN);
return 0;
}

View File

@@ -0,0 +1,285 @@
/*
* Atheros AR71xx built-in ethernet mac driver
*
* Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
*
* Based on Atheros' AG7100 driver
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/debugfs.h>
#include "ag71xx.h"
static struct dentry *ag71xx_debugfs_root;
static int ag71xx_debugfs_generic_open(struct inode *inode, struct file *file)
{
file->private_data = inode->i_private;
return 0;
}
void ag71xx_debugfs_update_int_stats(struct ag71xx *ag, u32 status)
{
if (status)
ag->debug.int_stats.total++;
if (status & AG71XX_INT_TX_PS)
ag->debug.int_stats.tx_ps++;
if (status & AG71XX_INT_TX_UR)
ag->debug.int_stats.tx_ur++;
if (status & AG71XX_INT_TX_BE)
ag->debug.int_stats.tx_be++;
if (status & AG71XX_INT_RX_PR)
ag->debug.int_stats.rx_pr++;
if (status & AG71XX_INT_RX_OF)
ag->debug.int_stats.rx_of++;
if (status & AG71XX_INT_RX_BE)
ag->debug.int_stats.rx_be++;
}
static ssize_t read_file_int_stats(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
#define PR_INT_STAT(_label, _field) \
len += snprintf(buf + len, sizeof(buf) - len, \
"%20s: %10lu\n", _label, ag->debug.int_stats._field);
struct ag71xx *ag = file->private_data;
char buf[256];
unsigned int len = 0;
PR_INT_STAT("TX Packet Sent", tx_ps);
PR_INT_STAT("TX Underrun", tx_ur);
PR_INT_STAT("TX Bus Error", tx_be);
PR_INT_STAT("RX Packet Received", rx_pr);
PR_INT_STAT("RX Overflow", rx_of);
PR_INT_STAT("RX Bus Error", rx_be);
len += snprintf(buf + len, sizeof(buf) - len, "\n");
PR_INT_STAT("Total", total);
return simple_read_from_buffer(user_buf, count, ppos, buf, len);
#undef PR_INT_STAT
}
static const struct file_operations ag71xx_fops_int_stats = {
.open = ag71xx_debugfs_generic_open,
.read = read_file_int_stats,
.owner = THIS_MODULE
};
void ag71xx_debugfs_update_napi_stats(struct ag71xx *ag, int rx, int tx)
{
struct ag71xx_napi_stats *stats = &ag->debug.napi_stats;
if (rx) {
stats->rx_count++;
stats->rx_packets += rx;
if (rx <= AG71XX_NAPI_WEIGHT)
stats->rx[rx]++;
if (rx > stats->rx_packets_max)
stats->rx_packets_max = rx;
}
if (tx) {
stats->tx_count++;
stats->tx_packets += tx;
if (tx <= AG71XX_NAPI_WEIGHT)
stats->tx[tx]++;
if (tx > stats->tx_packets_max)
stats->tx_packets_max = tx;
}
}
static ssize_t read_file_napi_stats(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
struct ag71xx *ag = file->private_data;
struct ag71xx_napi_stats *stats = &ag->debug.napi_stats;
char *buf;
unsigned int buflen;
unsigned int len = 0;
unsigned long rx_avg = 0;
unsigned long tx_avg = 0;
int ret;
int i;
buflen = 2048;
buf = kmalloc(buflen, GFP_KERNEL);
if (!buf)
return -ENOMEM;
if (stats->rx_count)
rx_avg = stats->rx_packets / stats->rx_count;
if (stats->tx_count)
tx_avg = stats->tx_packets / stats->tx_count;
len += snprintf(buf + len, buflen - len, "%3s %10s %10s\n",
"len", "rx", "tx");
for (i = 1; i <= AG71XX_NAPI_WEIGHT; i++)
len += snprintf(buf + len, buflen - len,
"%3d: %10lu %10lu\n",
i, stats->rx[i], stats->tx[i]);
len += snprintf(buf + len, buflen - len, "\n");
len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
"sum", stats->rx_count, stats->tx_count);
len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
"avg", rx_avg, tx_avg);
len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
"max", stats->rx_packets_max, stats->tx_packets_max);
len += snprintf(buf + len, buflen - len, "%3s: %10lu %10lu\n",
"pkt", stats->rx_packets, stats->tx_packets);
ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
kfree(buf);
return ret;
}
static const struct file_operations ag71xx_fops_napi_stats = {
.open = ag71xx_debugfs_generic_open,
.read = read_file_napi_stats,
.owner = THIS_MODULE
};
#define DESC_PRINT_LEN 64
static ssize_t read_file_ring(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos,
struct ag71xx *ag,
struct ag71xx_ring *ring,
unsigned desc_reg)
{
int ring_size = BIT(ring->order);
int ring_mask = ring_size - 1;
char *buf;
unsigned int buflen;
unsigned int len = 0;
unsigned long flags;
ssize_t ret;
int curr;
int dirty;
u32 desc_hw;
int i;
buflen = (ring_size * DESC_PRINT_LEN);
buf = kmalloc(buflen, GFP_KERNEL);
if (!buf)
return -ENOMEM;
len += snprintf(buf + len, buflen - len,
"Idx ... %-8s %-8s %-8s %-8s .\n",
"desc", "next", "data", "ctrl");
spin_lock_irqsave(&ag->lock, flags);
curr = (ring->curr & ring_mask);
dirty = (ring->dirty & ring_mask);
desc_hw = ag71xx_rr(ag, desc_reg);
for (i = 0; i < ring_size; i++) {
struct ag71xx_desc *desc = ag71xx_ring_desc(ring, i);
u32 desc_dma = ((u32) ring->descs_dma) + i * AG71XX_DESC_SIZE;
len += snprintf(buf + len, buflen - len,
"%3d %c%c%c %08x %08x %08x %08x %c\n",
i,
(i == curr) ? 'C' : ' ',
(i == dirty) ? 'D' : ' ',
(desc_hw == desc_dma) ? 'H' : ' ',
desc_dma,
desc->next,
desc->data,
desc->ctrl,
(desc->ctrl & DESC_EMPTY) ? 'E' : '*');
}
spin_unlock_irqrestore(&ag->lock, flags);
ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
kfree(buf);
return ret;
}
static ssize_t read_file_tx_ring(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
struct ag71xx *ag = file->private_data;
return read_file_ring(file, user_buf, count, ppos, ag, &ag->tx_ring,
AG71XX_REG_TX_DESC);
}
static const struct file_operations ag71xx_fops_tx_ring = {
.open = ag71xx_debugfs_generic_open,
.read = read_file_tx_ring,
.owner = THIS_MODULE
};
static ssize_t read_file_rx_ring(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
struct ag71xx *ag = file->private_data;
return read_file_ring(file, user_buf, count, ppos, ag, &ag->rx_ring,
AG71XX_REG_RX_DESC);
}
static const struct file_operations ag71xx_fops_rx_ring = {
.open = ag71xx_debugfs_generic_open,
.read = read_file_rx_ring,
.owner = THIS_MODULE
};
void ag71xx_debugfs_exit(struct ag71xx *ag)
{
debugfs_remove_recursive(ag->debug.debugfs_dir);
}
int ag71xx_debugfs_init(struct ag71xx *ag)
{
struct device *dev = &ag->pdev->dev;
ag->debug.debugfs_dir = debugfs_create_dir(dev_name(dev),
ag71xx_debugfs_root);
if (!ag->debug.debugfs_dir) {
dev_err(dev, "unable to create debugfs directory\n");
return -ENOENT;
}
debugfs_create_file("int_stats", S_IRUGO, ag->debug.debugfs_dir,
ag, &ag71xx_fops_int_stats);
debugfs_create_file("napi_stats", S_IRUGO, ag->debug.debugfs_dir,
ag, &ag71xx_fops_napi_stats);
debugfs_create_file("tx_ring", S_IRUGO, ag->debug.debugfs_dir,
ag, &ag71xx_fops_tx_ring);
debugfs_create_file("rx_ring", S_IRUGO, ag->debug.debugfs_dir,
ag, &ag71xx_fops_rx_ring);
return 0;
}
int ag71xx_debugfs_root_init(void)
{
if (ag71xx_debugfs_root)
return -EBUSY;
ag71xx_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
if (!ag71xx_debugfs_root)
return -ENOENT;
return 0;
}
void ag71xx_debugfs_root_exit(void)
{
debugfs_remove(ag71xx_debugfs_root);
ag71xx_debugfs_root = NULL;
}

View File

@@ -0,0 +1,131 @@
/*
* Atheros AR71xx built-in ethernet mac driver
*
* Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
*
* Based on Atheros' AG7100 driver
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "ag71xx.h"
static int ag71xx_ethtool_get_settings(struct net_device *dev,
struct ethtool_cmd *cmd)
{
struct ag71xx *ag = netdev_priv(dev);
struct phy_device *phydev = ag->phy_dev;
if (!phydev)
return -ENODEV;
return phy_ethtool_gset(phydev, cmd);
}
static int ag71xx_ethtool_set_settings(struct net_device *dev,
struct ethtool_cmd *cmd)
{
struct ag71xx *ag = netdev_priv(dev);
struct phy_device *phydev = ag->phy_dev;
if (!phydev)
return -ENODEV;
return phy_ethtool_sset(phydev, cmd);
}
static void ag71xx_ethtool_get_drvinfo(struct net_device *dev,
struct ethtool_drvinfo *info)
{
struct ag71xx *ag = netdev_priv(dev);
strcpy(info->driver, ag->pdev->dev.driver->name);
strcpy(info->version, AG71XX_DRV_VERSION);
strcpy(info->bus_info, dev_name(&ag->pdev->dev));
}
static u32 ag71xx_ethtool_get_msglevel(struct net_device *dev)
{
struct ag71xx *ag = netdev_priv(dev);
return ag->msg_enable;
}
static void ag71xx_ethtool_set_msglevel(struct net_device *dev, u32 msg_level)
{
struct ag71xx *ag = netdev_priv(dev);
ag->msg_enable = msg_level;
}
static void ag71xx_ethtool_get_ringparam(struct net_device *dev,
struct ethtool_ringparam *er)
{
struct ag71xx *ag = netdev_priv(dev);
er->tx_max_pending = AG71XX_TX_RING_SIZE_MAX;
er->rx_max_pending = AG71XX_RX_RING_SIZE_MAX;
er->rx_mini_max_pending = 0;
er->rx_jumbo_max_pending = 0;
er->tx_pending = BIT(ag->tx_ring.order);
er->rx_pending = BIT(ag->rx_ring.order);
er->rx_mini_pending = 0;
er->rx_jumbo_pending = 0;
if (ag->tx_ring.desc_split)
er->tx_pending /= AG71XX_TX_RING_DS_PER_PKT;
}
static int ag71xx_ethtool_set_ringparam(struct net_device *dev,
struct ethtool_ringparam *er)
{
struct ag71xx *ag = netdev_priv(dev);
unsigned tx_size;
unsigned rx_size;
int err = 0;
if (er->rx_mini_pending != 0||
er->rx_jumbo_pending != 0 ||
er->rx_pending == 0 ||
er->tx_pending == 0)
return -EINVAL;
tx_size = er->tx_pending < AG71XX_TX_RING_SIZE_MAX ?
er->tx_pending : AG71XX_TX_RING_SIZE_MAX;
rx_size = er->rx_pending < AG71XX_RX_RING_SIZE_MAX ?
er->rx_pending : AG71XX_RX_RING_SIZE_MAX;
if (netif_running(dev)) {
err = dev->netdev_ops->ndo_stop(dev);
if (err)
return err;
}
if (ag->tx_ring.desc_split)
tx_size *= AG71XX_TX_RING_DS_PER_PKT;
ag->tx_ring.order = ag71xx_ring_size_order(tx_size);
ag->rx_ring.order = ag71xx_ring_size_order(rx_size);
if (netif_running(dev))
err = dev->netdev_ops->ndo_open(dev);
return err;
}
struct ethtool_ops ag71xx_ethtool_ops = {
.set_settings = ag71xx_ethtool_set_settings,
.get_settings = ag71xx_ethtool_get_settings,
.get_drvinfo = ag71xx_ethtool_get_drvinfo,
.get_msglevel = ag71xx_ethtool_get_msglevel,
.set_msglevel = ag71xx_ethtool_set_msglevel,
.get_ringparam = ag71xx_ethtool_get_ringparam,
.set_ringparam = ag71xx_ethtool_set_ringparam,
.get_link = ethtool_op_get_link,
.get_ts_info = ethtool_op_get_ts_info,
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,320 @@
/*
* Atheros AR71xx built-in ethernet mac driver
*
* Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
*
* Based on Atheros' AG7100 driver
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "ag71xx.h"
#define AG71XX_MDIO_RETRY 1000
#define AG71XX_MDIO_DELAY 5
static inline void ag71xx_mdio_wr(struct ag71xx_mdio *am, unsigned reg,
u32 value)
{
void __iomem *r;
r = am->mdio_base + reg;
__raw_writel(value, r);
/* flush write */
(void) __raw_readl(r);
}
static inline u32 ag71xx_mdio_rr(struct ag71xx_mdio *am, unsigned reg)
{
return __raw_readl(am->mdio_base + reg);
}
static void ag71xx_mdio_dump_regs(struct ag71xx_mdio *am)
{
DBG("%s: mii_cfg=%08x, mii_cmd=%08x, mii_addr=%08x\n",
am->mii_bus->name,
ag71xx_mdio_rr(am, AG71XX_REG_MII_CFG),
ag71xx_mdio_rr(am, AG71XX_REG_MII_CMD),
ag71xx_mdio_rr(am, AG71XX_REG_MII_ADDR));
DBG("%s: mii_ctrl=%08x, mii_status=%08x, mii_ind=%08x\n",
am->mii_bus->name,
ag71xx_mdio_rr(am, AG71XX_REG_MII_CTRL),
ag71xx_mdio_rr(am, AG71XX_REG_MII_STATUS),
ag71xx_mdio_rr(am, AG71XX_REG_MII_IND));
}
static int ag71xx_mdio_wait_busy(struct ag71xx_mdio *am)
{
int i;
for (i = 0; i < AG71XX_MDIO_RETRY; i++) {
u32 busy;
udelay(AG71XX_MDIO_DELAY);
busy = ag71xx_mdio_rr(am, AG71XX_REG_MII_IND);
if (!busy)
return 0;
udelay(AG71XX_MDIO_DELAY);
}
pr_err("%s: MDIO operation timed out\n", am->mii_bus->name);
return -ETIMEDOUT;
}
int ag71xx_mdio_mii_read(struct ag71xx_mdio *am, int addr, int reg)
{
int err;
int ret;
err = ag71xx_mdio_wait_busy(am);
if (err)
return 0xffff;
ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_WRITE);
ag71xx_mdio_wr(am, AG71XX_REG_MII_ADDR,
((addr & 0xff) << MII_ADDR_SHIFT) | (reg & 0xff));
ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_READ);
err = ag71xx_mdio_wait_busy(am);
if (err)
return 0xffff;
ret = ag71xx_mdio_rr(am, AG71XX_REG_MII_STATUS) & 0xffff;
ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_WRITE);
DBG("mii_read: addr=%04x, reg=%04x, value=%04x\n", addr, reg, ret);
return ret;
}
void ag71xx_mdio_mii_write(struct ag71xx_mdio *am, int addr, int reg, u16 val)
{
DBG("mii_write: addr=%04x, reg=%04x, value=%04x\n", addr, reg, val);
ag71xx_mdio_wr(am, AG71XX_REG_MII_ADDR,
((addr & 0xff) << MII_ADDR_SHIFT) | (reg & 0xff));
ag71xx_mdio_wr(am, AG71XX_REG_MII_CTRL, val);
ag71xx_mdio_wait_busy(am);
}
static const u32 ar71xx_mdio_div_table[] = {
4, 4, 6, 8, 10, 14, 20, 28,
};
static const u32 ar7240_mdio_div_table[] = {
2, 2, 4, 6, 8, 12, 18, 26, 32, 40, 48, 56, 62, 70, 78, 96,
};
static const u32 ar933x_mdio_div_table[] = {
4, 4, 6, 8, 10, 14, 20, 28, 34, 42, 50, 58, 66, 74, 82, 98,
};
static int ag71xx_mdio_get_divider(struct ag71xx_mdio *am, u32 *div)
{
unsigned long ref_clock, mdio_clock;
const u32 *table;
int ndivs;
int i;
ref_clock = am->pdata->ref_clock;
mdio_clock = am->pdata->mdio_clock;
if (!ref_clock || !mdio_clock)
return -EINVAL;
if (am->pdata->is_ar9330 || am->pdata->is_ar934x) {
table = ar933x_mdio_div_table;
ndivs = ARRAY_SIZE(ar933x_mdio_div_table);
} else if (am->pdata->is_ar7240) {
table = ar7240_mdio_div_table;
ndivs = ARRAY_SIZE(ar7240_mdio_div_table);
} else {
table = ar71xx_mdio_div_table;
ndivs = ARRAY_SIZE(ar71xx_mdio_div_table);
}
for (i = 0; i < ndivs; i++) {
unsigned long t;
t = ref_clock / table[i];
if (t <= mdio_clock) {
*div = i;
return 0;
}
}
dev_err(&am->mii_bus->dev, "no divider found for %lu/%lu\n",
ref_clock, mdio_clock);
return -ENOENT;
}
static int ag71xx_mdio_reset(struct mii_bus *bus)
{
struct ag71xx_mdio *am = bus->priv;
u32 t;
int err;
err = ag71xx_mdio_get_divider(am, &t);
if (err) {
/* fallback */
if (am->pdata->is_ar7240)
t = MII_CFG_CLK_DIV_6;
else if (am->pdata->builtin_switch && !am->pdata->is_ar934x)
t = MII_CFG_CLK_DIV_10;
else if (!am->pdata->builtin_switch && am->pdata->is_ar934x)
t = MII_CFG_CLK_DIV_58;
else
t = MII_CFG_CLK_DIV_28;
}
ag71xx_mdio_wr(am, AG71XX_REG_MII_CFG, t | MII_CFG_RESET);
udelay(100);
ag71xx_mdio_wr(am, AG71XX_REG_MII_CFG, t);
udelay(100);
if (am->pdata->reset)
am->pdata->reset(bus);
return 0;
}
static int ag71xx_mdio_read(struct mii_bus *bus, int addr, int reg)
{
struct ag71xx_mdio *am = bus->priv;
if (am->pdata->builtin_switch)
return ar7240sw_phy_read(bus, addr, reg);
else
return ag71xx_mdio_mii_read(am, addr, reg);
}
static int ag71xx_mdio_write(struct mii_bus *bus, int addr, int reg, u16 val)
{
struct ag71xx_mdio *am = bus->priv;
if (am->pdata->builtin_switch)
ar7240sw_phy_write(bus, addr, reg, val);
else
ag71xx_mdio_mii_write(am, addr, reg, val);
return 0;
}
static int ag71xx_mdio_probe(struct platform_device *pdev)
{
struct ag71xx_mdio_platform_data *pdata;
struct ag71xx_mdio *am;
struct resource *res;
int i;
int err;
pdata = pdev->dev.platform_data;
if (!pdata) {
dev_err(&pdev->dev, "no platform data specified\n");
return -EINVAL;
}
am = kzalloc(sizeof(*am), GFP_KERNEL);
if (!am) {
err = -ENOMEM;
goto err_out;
}
am->pdata = pdata;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
dev_err(&pdev->dev, "no iomem resource found\n");
err = -ENXIO;
goto err_free_mdio;
}
am->mdio_base = ioremap_nocache(res->start, res->end - res->start + 1);
if (!am->mdio_base) {
dev_err(&pdev->dev, "unable to ioremap registers\n");
err = -ENOMEM;
goto err_free_mdio;
}
am->mii_bus = mdiobus_alloc();
if (am->mii_bus == NULL) {
err = -ENOMEM;
goto err_iounmap;
}
am->mii_bus->name = "ag71xx_mdio";
am->mii_bus->read = ag71xx_mdio_read;
am->mii_bus->write = ag71xx_mdio_write;
am->mii_bus->reset = ag71xx_mdio_reset;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
am->mii_bus->irq = am->mii_irq;
#endif
am->mii_bus->priv = am;
am->mii_bus->parent = &pdev->dev;
snprintf(am->mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(&pdev->dev));
am->mii_bus->phy_mask = pdata->phy_mask;
for (i = 0; i < PHY_MAX_ADDR; i++)
am->mii_bus->irq[i] = PHY_POLL;
ag71xx_mdio_wr(am, AG71XX_REG_MAC_CFG1, 0);
err = mdiobus_register(am->mii_bus);
if (err)
goto err_free_bus;
ag71xx_mdio_dump_regs(am);
platform_set_drvdata(pdev, am);
return 0;
err_free_bus:
mdiobus_free(am->mii_bus);
err_iounmap:
iounmap(am->mdio_base);
err_free_mdio:
kfree(am);
err_out:
return err;
}
static int ag71xx_mdio_remove(struct platform_device *pdev)
{
struct ag71xx_mdio *am = platform_get_drvdata(pdev);
if (am) {
mdiobus_unregister(am->mii_bus);
mdiobus_free(am->mii_bus);
iounmap(am->mdio_base);
kfree(am);
platform_set_drvdata(pdev, NULL);
}
return 0;
}
static struct platform_driver ag71xx_mdio_driver = {
.probe = ag71xx_mdio_probe,
.remove = ag71xx_mdio_remove,
.driver = {
.name = "ag71xx-mdio",
}
};
int __init ag71xx_mdio_driver_init(void)
{
return platform_driver_register(&ag71xx_mdio_driver);
}
void ag71xx_mdio_driver_exit(void)
{
platform_driver_unregister(&ag71xx_mdio_driver);
}

View File

@@ -0,0 +1,261 @@
/*
* Atheros AR71xx built-in ethernet mac driver
*
* Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
*
* Based on Atheros' AG7100 driver
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "ag71xx.h"
static void ag71xx_phy_link_adjust(struct net_device *dev)
{
struct ag71xx *ag = netdev_priv(dev);
struct phy_device *phydev = ag->phy_dev;
unsigned long flags;
int status_change = 0;
spin_lock_irqsave(&ag->lock, flags);
if (phydev->link) {
if (ag->duplex != phydev->duplex
|| ag->speed != phydev->speed) {
status_change = 1;
}
}
if (phydev->link != ag->link)
status_change = 1;
ag->link = phydev->link;
ag->duplex = phydev->duplex;
ag->speed = phydev->speed;
if (status_change)
ag71xx_link_adjust(ag);
spin_unlock_irqrestore(&ag->lock, flags);
}
void ag71xx_phy_start(struct ag71xx *ag)
{
struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
if (ag->phy_dev) {
phy_start(ag->phy_dev);
} else if (pdata->mii_bus_dev && pdata->switch_data) {
ag71xx_ar7240_start(ag);
} else {
ag->link = 1;
ag71xx_link_adjust(ag);
}
}
void ag71xx_phy_stop(struct ag71xx *ag)
{
struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
unsigned long flags;
if (ag->phy_dev)
phy_stop(ag->phy_dev);
else if (pdata->mii_bus_dev && pdata->switch_data)
ag71xx_ar7240_stop(ag);
spin_lock_irqsave(&ag->lock, flags);
if (ag->link) {
ag->link = 0;
ag71xx_link_adjust(ag);
}
spin_unlock_irqrestore(&ag->lock, flags);
}
static int ag71xx_phy_connect_fixed(struct ag71xx *ag)
{
struct device *dev = &ag->pdev->dev;
struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
int ret = 0;
/* use fixed settings */
switch (pdata->speed) {
case SPEED_10:
case SPEED_100:
case SPEED_1000:
break;
default:
dev_err(dev, "invalid speed specified\n");
ret = -EINVAL;
break;
}
dev_dbg(dev, "using fixed link parameters\n");
ag->duplex = pdata->duplex;
ag->speed = pdata->speed;
return ret;
}
static int ag71xx_phy_connect_multi(struct ag71xx *ag)
{
struct device *dev = &ag->pdev->dev;
struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
struct phy_device *phydev = NULL;
int phy_addr;
int ret = 0;
for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
if (!(pdata->phy_mask & (1 << phy_addr)))
continue;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
if (ag->mii_bus->phy_map[phy_addr] == NULL)
continue;
DBG("%s: PHY found at %s, uid=%08x\n",
dev_name(dev),
dev_name(&ag->mii_bus->phy_map[phy_addr]->dev),
ag->mii_bus->phy_map[phy_addr]->phy_id);
if (phydev == NULL)
phydev = ag->mii_bus->phy_map[phy_addr];
#else
if (ag->mii_bus->mdio_map[phy_addr] == NULL)
continue;
DBG("%s: PHY found at %s, uid=%08x\n",
dev_name(dev),
dev_name(&ag->mii_bus->mdio_map[phy_addr]->dev),
ag->mii_bus->mdio_map[phy_addr]->phy_id);
if (phydev == NULL)
phydev = mdiobus_get_phy(ag->mii_bus, phy_addr);
#endif
}
if (!phydev) {
dev_err(dev, "no PHY found with phy_mask=%08x\n",
pdata->phy_mask);
return -ENODEV;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
ag->phy_dev = phy_connect(ag->dev, dev_name(&phydev->dev),
#else
ag->phy_dev = phy_connect(ag->dev, phydev_name(phydev),
#endif
&ag71xx_phy_link_adjust,
pdata->phy_if_mode);
if (IS_ERR(ag->phy_dev)) {
dev_err(dev, "could not connect to PHY at %s\n",
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
dev_name(&phydev->dev));
#else
phydev_name(phydev));
#endif
return PTR_ERR(ag->phy_dev);
}
/* mask with MAC supported features */
if (pdata->has_gbit)
phydev->supported &= PHY_GBIT_FEATURES;
else
phydev->supported &= PHY_BASIC_FEATURES;
phydev->advertising = phydev->supported;
dev_info(dev, "connected to PHY at %s [uid=%08x, driver=%s]\n",
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
dev_name(&phydev->dev),
#else
phydev_name(phydev),
#endif
phydev->phy_id, phydev->drv->name);
ag->link = 0;
ag->speed = 0;
ag->duplex = -1;
return ret;
}
static int dev_is_class(struct device *dev, void *class)
{
if (dev->class != NULL && !strcmp(dev->class->name, class))
return 1;
return 0;
}
static struct device *dev_find_class(struct device *parent, char *class)
{
if (dev_is_class(parent, class)) {
get_device(parent);
return parent;
}
return device_find_child(parent, class, dev_is_class);
}
static struct mii_bus *dev_to_mii_bus(struct device *dev)
{
struct device *d;
d = dev_find_class(dev, "mdio_bus");
if (d != NULL) {
struct mii_bus *bus;
bus = to_mii_bus(d);
put_device(d);
return bus;
}
return NULL;
}
int ag71xx_phy_connect(struct ag71xx *ag)
{
struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
if (pdata->mii_bus_dev == NULL ||
pdata->mii_bus_dev->bus == NULL )
return ag71xx_phy_connect_fixed(ag);
ag->mii_bus = dev_to_mii_bus(pdata->mii_bus_dev);
if (ag->mii_bus == NULL) {
dev_err(&ag->pdev->dev, "unable to find MII bus on device '%s'\n",
dev_name(pdata->mii_bus_dev));
return -ENODEV;
}
/* Reset the mdio bus explicitly */
if (ag->mii_bus->reset) {
mutex_lock(&ag->mii_bus->mdio_lock);
ag->mii_bus->reset(ag->mii_bus);
mutex_unlock(&ag->mii_bus->mdio_lock);
}
if (pdata->switch_data)
return ag71xx_ar7240_init(ag);
if (pdata->phy_mask)
return ag71xx_phy_connect_multi(ag);
return ag71xx_phy_connect_fixed(ag);
}
void ag71xx_phy_disconnect(struct ag71xx *ag)
{
struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
if (pdata->switch_data)
ag71xx_ar7240_cleanup(ag);
else if (ag->phy_dev)
phy_disconnect(ag->phy_dev);
}

View File

@@ -0,0 +1,347 @@
/*
* SPI driver for the CPLD chip on the Mikrotik RB4xx boards
*
* Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
*
* This file was based on the patches for Linux 2.6.27.39 published by
* MikroTik for their RouterBoard 4xx series devices.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/bitops.h>
#include <linux/spi/spi.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <asm/mach-ath79/rb4xx_cpld.h>
#define DRV_NAME "spi-rb4xx-cpld"
#define DRV_DESC "RB4xx CPLD driver"
#define DRV_VERSION "0.1.0"
#define CPLD_CMD_WRITE_NAND 0x08 /* send cmd, n x send data, send indle */
#define CPLD_CMD_WRITE_CFG 0x09 /* send cmd, n x send cfg */
#define CPLD_CMD_READ_NAND 0x0a /* send cmd, send idle, n x read data */
#define CPLD_CMD_READ_FAST 0x0b /* send cmd, 4 x idle, n x read data */
#define CPLD_CMD_LED5_ON 0x0c /* send cmd */
#define CPLD_CMD_LED5_OFF 0x0d /* send cmd */
struct rb4xx_cpld {
struct spi_device *spi;
struct mutex lock;
struct gpio_chip chip;
unsigned int config;
};
static struct rb4xx_cpld *rb4xx_cpld;
static inline struct rb4xx_cpld *gpio_to_cpld(struct gpio_chip *chip)
{
return container_of(chip, struct rb4xx_cpld, chip);
}
static int rb4xx_cpld_write_cmd(struct rb4xx_cpld *cpld, unsigned char cmd)
{
struct spi_transfer t[1];
struct spi_message m;
unsigned char tx_buf[1];
int err;
spi_message_init(&m);
memset(&t, 0, sizeof(t));
t[0].tx_buf = tx_buf;
t[0].len = sizeof(tx_buf);
spi_message_add_tail(&t[0], &m);
tx_buf[0] = cmd;
err = spi_sync(cpld->spi, &m);
return err;
}
static int rb4xx_cpld_write_cfg(struct rb4xx_cpld *cpld, unsigned char config)
{
struct spi_transfer t[1];
struct spi_message m;
unsigned char cmd[2];
int err;
spi_message_init(&m);
memset(&t, 0, sizeof(t));
t[0].tx_buf = cmd;
t[0].len = sizeof(cmd);
spi_message_add_tail(&t[0], &m);
cmd[0] = CPLD_CMD_WRITE_CFG;
cmd[1] = config;
err = spi_sync(cpld->spi, &m);
return err;
}
static int __rb4xx_cpld_change_cfg(struct rb4xx_cpld *cpld, unsigned mask,
unsigned value)
{
unsigned int config;
int err;
config = cpld->config & ~mask;
config |= value;
if ((cpld->config ^ config) & 0xff) {
err = rb4xx_cpld_write_cfg(cpld, config);
if (err)
return err;
}
if ((cpld->config ^ config) & CPLD_CFG_nLED5) {
err = rb4xx_cpld_write_cmd(cpld, (value) ? CPLD_CMD_LED5_ON :
CPLD_CMD_LED5_OFF);
if (err)
return err;
}
cpld->config = config;
return 0;
}
int rb4xx_cpld_change_cfg(unsigned mask, unsigned value)
{
int ret;
if (rb4xx_cpld == NULL)
return -ENODEV;
mutex_lock(&rb4xx_cpld->lock);
ret = __rb4xx_cpld_change_cfg(rb4xx_cpld, mask, value);
mutex_unlock(&rb4xx_cpld->lock);
return ret;
}
EXPORT_SYMBOL_GPL(rb4xx_cpld_change_cfg);
int rb4xx_cpld_read(unsigned char *rx_buf, unsigned count)
{
static const unsigned char cmd[2] = { CPLD_CMD_READ_NAND, 0 };
struct spi_transfer t[2] = {
{
.tx_buf = &cmd,
.len = 2,
}, {
.rx_buf = rx_buf,
.len = count,
},
};
struct spi_message m;
if (rb4xx_cpld == NULL)
return -ENODEV;
spi_message_init(&m);
spi_message_add_tail(&t[0], &m);
spi_message_add_tail(&t[1], &m);
return spi_sync(rb4xx_cpld->spi, &m);
}
EXPORT_SYMBOL_GPL(rb4xx_cpld_read);
int rb4xx_cpld_write(const unsigned char *buf, unsigned count)
{
static const unsigned char cmd = CPLD_CMD_WRITE_NAND;
struct spi_transfer t[3] = {
{
.tx_buf = &cmd,
.len = 1,
}, {
.tx_buf = buf,
.len = count,
.tx_nbits = SPI_NBITS_DUAL,
}, {
.len = 1,
.tx_nbits = SPI_NBITS_DUAL,
},
};
struct spi_message m;
if (rb4xx_cpld == NULL)
return -ENODEV;
spi_message_init(&m);
spi_message_add_tail(&t[0], &m);
spi_message_add_tail(&t[1], &m);
spi_message_add_tail(&t[2], &m);
return spi_sync(rb4xx_cpld->spi, &m);
}
EXPORT_SYMBOL_GPL(rb4xx_cpld_write);
static int rb4xx_cpld_gpio_get(struct gpio_chip *chip, unsigned offset)
{
struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
int ret;
mutex_lock(&cpld->lock);
ret = (cpld->config >> offset) & 1;
mutex_unlock(&cpld->lock);
return ret;
}
static void rb4xx_cpld_gpio_set(struct gpio_chip *chip, unsigned offset,
int value)
{
struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
mutex_lock(&cpld->lock);
__rb4xx_cpld_change_cfg(cpld, (1 << offset), !!value << offset);
mutex_unlock(&cpld->lock);
}
static int rb4xx_cpld_gpio_direction_input(struct gpio_chip *chip,
unsigned offset)
{
return -EOPNOTSUPP;
}
static int rb4xx_cpld_gpio_direction_output(struct gpio_chip *chip,
unsigned offset,
int value)
{
struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
int ret;
mutex_lock(&cpld->lock);
ret = __rb4xx_cpld_change_cfg(cpld, (1 << offset), !!value << offset);
mutex_unlock(&cpld->lock);
return ret;
}
static int rb4xx_cpld_gpio_init(struct rb4xx_cpld *cpld, unsigned int base)
{
int err;
/* init config */
cpld->config = CPLD_CFG_nLED1 | CPLD_CFG_nLED2 | CPLD_CFG_nLED3 |
CPLD_CFG_nLED4 | CPLD_CFG_nCE;
rb4xx_cpld_write_cfg(cpld, cpld->config);
/* setup GPIO chip */
cpld->chip.label = DRV_NAME;
cpld->chip.get = rb4xx_cpld_gpio_get;
cpld->chip.set = rb4xx_cpld_gpio_set;
cpld->chip.direction_input = rb4xx_cpld_gpio_direction_input;
cpld->chip.direction_output = rb4xx_cpld_gpio_direction_output;
cpld->chip.base = base;
cpld->chip.ngpio = CPLD_NUM_GPIOS;
cpld->chip.can_sleep = 1;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
cpld->chip.dev = &cpld->spi->dev;
#else
cpld->chip.parent = &cpld->spi->dev;
#endif
cpld->chip.owner = THIS_MODULE;
err = gpiochip_add(&cpld->chip);
if (err)
dev_err(&cpld->spi->dev, "adding GPIO chip failed, err=%d\n",
err);
return err;
}
static int rb4xx_cpld_probe(struct spi_device *spi)
{
struct rb4xx_cpld *cpld;
struct rb4xx_cpld_platform_data *pdata;
int err;
pdata = spi->dev.platform_data;
if (!pdata) {
dev_dbg(&spi->dev, "no platform data\n");
return -EINVAL;
}
cpld = kzalloc(sizeof(*cpld), GFP_KERNEL);
if (!cpld) {
dev_err(&spi->dev, "no memory for private data\n");
return -ENOMEM;
}
mutex_init(&cpld->lock);
cpld->spi = spi_dev_get(spi);
dev_set_drvdata(&spi->dev, cpld);
spi->mode = SPI_MODE_0 | SPI_TX_DUAL;
spi->bits_per_word = 8;
err = spi_setup(spi);
if (err) {
dev_err(&spi->dev, "spi_setup failed, err=%d\n", err);
goto err_drvdata;
}
err = rb4xx_cpld_gpio_init(cpld, pdata->gpio_base);
if (err)
goto err_drvdata;
rb4xx_cpld = cpld;
return 0;
err_drvdata:
dev_set_drvdata(&spi->dev, NULL);
kfree(cpld);
return err;
}
static int rb4xx_cpld_remove(struct spi_device *spi)
{
struct rb4xx_cpld *cpld;
rb4xx_cpld = NULL;
cpld = dev_get_drvdata(&spi->dev);
dev_set_drvdata(&spi->dev, NULL);
kfree(cpld);
return 0;
}
static struct spi_driver rb4xx_cpld_driver = {
.driver = {
.name = DRV_NAME,
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
.probe = rb4xx_cpld_probe,
.remove = rb4xx_cpld_remove,
};
static int __init rb4xx_cpld_init(void)
{
return spi_register_driver(&rb4xx_cpld_driver);
}
module_init(rb4xx_cpld_init);
static void __exit rb4xx_cpld_exit(void)
{
spi_unregister_driver(&rb4xx_cpld_driver);
}
module_exit(rb4xx_cpld_exit);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");

View File

@@ -0,0 +1,430 @@
/*
* SPI controller driver for the Mikrotik RB4xx boards
*
* Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
*
* This file was based on the patches for Linux 2.6.27.39 published by
* MikroTik for their RouterBoard 4xx series devices.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <asm/mach-ath79/ar71xx_regs.h>
#include <asm/mach-ath79/ath79.h>
#define DRV_NAME "rb4xx-spi"
#define DRV_DESC "Mikrotik RB4xx SPI controller driver"
#define DRV_VERSION "0.1.0"
#define SPI_CTRL_FASTEST 0x40
#define SPI_FLASH_HZ 33333334
#define SPI_CPLD_HZ 33333334
#define CPLD_CMD_READ_FAST 0x0b
#undef RB4XX_SPI_DEBUG
struct rb4xx_spi {
void __iomem *base;
struct spi_master *master;
unsigned spi_ctrl_flash;
unsigned spi_ctrl_fread;
struct clk *ahb_clk;
unsigned long ahb_freq;
spinlock_t lock;
struct list_head queue;
int busy:1;
int cs_wait;
};
static unsigned spi_clk_low = AR71XX_SPI_IOC_CS1;
#ifdef RB4XX_SPI_DEBUG
static inline void do_spi_delay(void)
{
ndelay(20000);
}
#else
static inline void do_spi_delay(void) { }
#endif
static inline void do_spi_init(struct spi_device *spi)
{
unsigned cs = AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1;
if (!(spi->mode & SPI_CS_HIGH))
cs ^= (spi->chip_select == 2) ? AR71XX_SPI_IOC_CS1 :
AR71XX_SPI_IOC_CS0;
spi_clk_low = cs;
}
static inline void do_spi_finish(void __iomem *base)
{
do_spi_delay();
__raw_writel(AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1,
base + AR71XX_SPI_REG_IOC);
}
static inline void do_spi_clk(void __iomem *base, int bit)
{
unsigned bval = spi_clk_low | ((bit & 1) ? AR71XX_SPI_IOC_DO : 0);
do_spi_delay();
__raw_writel(bval, base + AR71XX_SPI_REG_IOC);
do_spi_delay();
__raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
}
static void do_spi_byte(void __iomem *base, unsigned char byte)
{
do_spi_clk(base, byte >> 7);
do_spi_clk(base, byte >> 6);
do_spi_clk(base, byte >> 5);
do_spi_clk(base, byte >> 4);
do_spi_clk(base, byte >> 3);
do_spi_clk(base, byte >> 2);
do_spi_clk(base, byte >> 1);
do_spi_clk(base, byte);
pr_debug("spi_byte sent 0x%02x got 0x%02x\n",
(unsigned)byte,
(unsigned char)__raw_readl(base + AR71XX_SPI_REG_RDS));
}
static inline void do_spi_clk_fast(void __iomem *base, unsigned bit1,
unsigned bit2)
{
unsigned bval = (spi_clk_low |
((bit1 & 1) ? AR71XX_SPI_IOC_DO : 0) |
((bit2 & 1) ? AR71XX_SPI_IOC_CS2 : 0));
do_spi_delay();
__raw_writel(bval, base + AR71XX_SPI_REG_IOC);
do_spi_delay();
__raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
}
static void do_spi_byte_fast(void __iomem *base, unsigned char byte)
{
do_spi_clk_fast(base, byte >> 7, byte >> 6);
do_spi_clk_fast(base, byte >> 5, byte >> 4);
do_spi_clk_fast(base, byte >> 3, byte >> 2);
do_spi_clk_fast(base, byte >> 1, byte >> 0);
pr_debug("spi_byte_fast sent 0x%02x got 0x%02x\n",
(unsigned)byte,
(unsigned char) __raw_readl(base + AR71XX_SPI_REG_RDS));
}
static int rb4xx_spi_txrx(void __iomem *base, struct spi_transfer *t)
{
const unsigned char *tx_ptr = t->tx_buf;
unsigned char *rx_ptr = t->rx_buf;
unsigned i;
pr_debug("spi_txrx len %u tx %u rx %u\n",
t->len,
(t->tx_buf ? 1 : 0),
(t->rx_buf ? 1 : 0));
for (i = 0; i < t->len; ++i) {
unsigned char sdata = tx_ptr ? tx_ptr[i] : 0;
if (t->tx_nbits == SPI_NBITS_DUAL)
do_spi_byte_fast(base, sdata);
else
do_spi_byte(base, sdata);
if (rx_ptr)
rx_ptr[i] = __raw_readl(base + AR71XX_SPI_REG_RDS) & 0xff;
}
return i;
}
static int rb4xx_spi_msg(struct rb4xx_spi *rbspi, struct spi_message *m)
{
struct spi_transfer *t = NULL;
void __iomem *base = rbspi->base;
m->status = 0;
if (list_empty(&m->transfers))
return -1;
__raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
__raw_writel(SPI_CTRL_FASTEST, base + AR71XX_SPI_REG_CTRL);
do_spi_init(m->spi);
list_for_each_entry(t, &m->transfers, transfer_list) {
int len;
len = rb4xx_spi_txrx(base, t);
if (len != t->len) {
m->status = -EMSGSIZE;
break;
}
m->actual_length += len;
if (t->cs_change) {
if (list_is_last(&t->transfer_list, &m->transfers)) {
/* wait for continuation */
return m->spi->chip_select;
}
do_spi_finish(base);
ndelay(100);
}
}
do_spi_finish(base);
__raw_writel(rbspi->spi_ctrl_flash, base + AR71XX_SPI_REG_CTRL);
__raw_writel(0, base + AR71XX_SPI_REG_FS);
return -1;
}
static void rb4xx_spi_process_queue_locked(struct rb4xx_spi *rbspi,
unsigned long *flags)
{
int cs = rbspi->cs_wait;
rbspi->busy = 1;
while (!list_empty(&rbspi->queue)) {
struct spi_message *m;
list_for_each_entry(m, &rbspi->queue, queue)
if (cs < 0 || cs == m->spi->chip_select)
break;
if (&m->queue == &rbspi->queue)
break;
list_del_init(&m->queue);
spin_unlock_irqrestore(&rbspi->lock, *flags);
cs = rb4xx_spi_msg(rbspi, m);
m->complete(m->context);
spin_lock_irqsave(&rbspi->lock, *flags);
}
rbspi->cs_wait = cs;
rbspi->busy = 0;
if (cs >= 0) {
/* TODO: add timer to unlock cs after 1s inactivity */
}
}
static int rb4xx_spi_transfer(struct spi_device *spi,
struct spi_message *m)
{
struct rb4xx_spi *rbspi = spi_master_get_devdata(spi->master);
unsigned long flags;
m->actual_length = 0;
m->status = -EINPROGRESS;
spin_lock_irqsave(&rbspi->lock, flags);
list_add_tail(&m->queue, &rbspi->queue);
if (rbspi->busy ||
(rbspi->cs_wait >= 0 && rbspi->cs_wait != m->spi->chip_select)) {
/* job will be done later */
spin_unlock_irqrestore(&rbspi->lock, flags);
return 0;
}
/* process job in current context */
rb4xx_spi_process_queue_locked(rbspi, &flags);
spin_unlock_irqrestore(&rbspi->lock, flags);
return 0;
}
static int rb4xx_spi_setup(struct spi_device *spi)
{
struct rb4xx_spi *rbspi = spi_master_get_devdata(spi->master);
unsigned long flags;
if (spi->mode & ~(SPI_CS_HIGH | SPI_TX_DUAL)) {
dev_err(&spi->dev, "mode %x not supported\n",
(unsigned) spi->mode);
return -EINVAL;
}
if (spi->bits_per_word != 8 && spi->bits_per_word != 0) {
dev_err(&spi->dev, "bits_per_word %u not supported\n",
(unsigned) spi->bits_per_word);
return -EINVAL;
}
spin_lock_irqsave(&rbspi->lock, flags);
if (rbspi->cs_wait == spi->chip_select && !rbspi->busy) {
rbspi->cs_wait = -1;
rb4xx_spi_process_queue_locked(rbspi, &flags);
}
spin_unlock_irqrestore(&rbspi->lock, flags);
return 0;
}
static unsigned get_spi_ctrl(struct rb4xx_spi *rbspi, unsigned hz_max,
const char *name)
{
unsigned div;
div = (rbspi->ahb_freq - 1) / (2 * hz_max);
/*
* CPU has a bug at (div == 0) - first bit read is random
*/
if (div == 0)
++div;
if (name) {
unsigned ahb_khz = (rbspi->ahb_freq + 500) / 1000;
unsigned div_real = 2 * (div + 1);
pr_debug("rb4xx: %s SPI clock %u kHz (AHB %u kHz / %u)\n",
name,
ahb_khz / div_real,
ahb_khz, div_real);
}
return SPI_CTRL_FASTEST + div;
}
static int rb4xx_spi_probe(struct platform_device *pdev)
{
struct spi_master *master;
struct rb4xx_spi *rbspi;
struct resource *r;
int err = 0;
master = spi_alloc_master(&pdev->dev, sizeof(*rbspi));
if (master == NULL) {
dev_err(&pdev->dev, "no memory for spi_master\n");
err = -ENOMEM;
goto err_out;
}
master->bus_num = 0;
master->num_chipselect = 3;
master->mode_bits = SPI_TX_DUAL;
master->setup = rb4xx_spi_setup;
master->transfer = rb4xx_spi_transfer;
rbspi = spi_master_get_devdata(master);
rbspi->ahb_clk = clk_get(&pdev->dev, "ahb");
if (IS_ERR(rbspi->ahb_clk)) {
err = PTR_ERR(rbspi->ahb_clk);
goto err_put_master;
}
err = clk_prepare_enable(rbspi->ahb_clk);
if (err)
goto err_clk_put;
rbspi->ahb_freq = clk_get_rate(rbspi->ahb_clk);
if (!rbspi->ahb_freq) {
err = -EINVAL;
goto err_clk_disable;
}
platform_set_drvdata(pdev, rbspi);
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (r == NULL) {
err = -ENOENT;
goto err_clk_disable;
}
rbspi->base = ioremap(r->start, r->end - r->start + 1);
if (!rbspi->base) {
err = -ENXIO;
goto err_clk_disable;
}
rbspi->master = master;
rbspi->spi_ctrl_flash = get_spi_ctrl(rbspi, SPI_FLASH_HZ, "FLASH");
rbspi->spi_ctrl_fread = get_spi_ctrl(rbspi, SPI_CPLD_HZ, "CPLD");
rbspi->cs_wait = -1;
spin_lock_init(&rbspi->lock);
INIT_LIST_HEAD(&rbspi->queue);
err = spi_register_master(master);
if (err) {
dev_err(&pdev->dev, "failed to register SPI master\n");
goto err_iounmap;
}
return 0;
err_iounmap:
iounmap(rbspi->base);
err_clk_disable:
clk_disable_unprepare(rbspi->ahb_clk);
err_clk_put:
clk_put(rbspi->ahb_clk);
err_put_master:
platform_set_drvdata(pdev, NULL);
spi_master_put(master);
err_out:
return err;
}
static int rb4xx_spi_remove(struct platform_device *pdev)
{
struct rb4xx_spi *rbspi = platform_get_drvdata(pdev);
iounmap(rbspi->base);
clk_disable_unprepare(rbspi->ahb_clk);
clk_put(rbspi->ahb_clk);
platform_set_drvdata(pdev, NULL);
spi_master_put(rbspi->master);
return 0;
}
static struct platform_driver rb4xx_spi_drv = {
.probe = rb4xx_spi_probe,
.remove = rb4xx_spi_remove,
.driver = {
.name = DRV_NAME,
.owner = THIS_MODULE,
},
};
static int __init rb4xx_spi_init(void)
{
return platform_driver_register(&rb4xx_spi_drv);
}
subsys_initcall(rb4xx_spi_init);
static void __exit rb4xx_spi_exit(void)
{
platform_driver_unregister(&rb4xx_spi_drv);
}
module_exit(rb4xx_spi_exit);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");

View File

@@ -0,0 +1,621 @@
/*
* SPI driver for the Vitesse VSC7385 ethernet switch
*
* Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
*
* Parts of this file are based on Atheros' 2.6.15 BSP
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/bitops.h>
#include <linux/firmware.h>
#include <linux/spi/spi.h>
#include <linux/spi/vsc7385.h>
#define DRV_NAME "spi-vsc7385"
#define DRV_DESC "Vitesse VSC7385 Gbit ethernet switch driver"
#define DRV_VERSION "0.1.0"
#define VSC73XX_BLOCK_MAC 0x1
#define VSC73XX_BLOCK_2 0x2
#define VSC73XX_BLOCK_MII 0x3
#define VSC73XX_BLOCK_4 0x4
#define VSC73XX_BLOCK_5 0x5
#define VSC73XX_BLOCK_SYSTEM 0x7
#define VSC73XX_SUBBLOCK_PORT_0 0
#define VSC73XX_SUBBLOCK_PORT_1 1
#define VSC73XX_SUBBLOCK_PORT_2 2
#define VSC73XX_SUBBLOCK_PORT_3 3
#define VSC73XX_SUBBLOCK_PORT_4 4
#define VSC73XX_SUBBLOCK_PORT_MAC 6
/* MAC Block registers */
#define VSC73XX_MAC_CFG 0x0
#define VSC73XX_ADVPORTM 0x19
#define VSC73XX_RXOCT 0x50
#define VSC73XX_TXOCT 0x51
#define VSC73XX_C_RX0 0x52
#define VSC73XX_C_RX1 0x53
#define VSC73XX_C_RX2 0x54
#define VSC73XX_C_TX0 0x55
#define VSC73XX_C_TX1 0x56
#define VSC73XX_C_TX2 0x57
#define VSC73XX_C_CFG 0x58
/* MAC_CFG register bits */
#define VSC73XX_MAC_CFG_WEXC_DIS (1 << 31)
#define VSC73XX_MAC_CFG_PORT_RST (1 << 29)
#define VSC73XX_MAC_CFG_TX_EN (1 << 28)
#define VSC73XX_MAC_CFG_SEED_LOAD (1 << 27)
#define VSC73XX_MAC_CFG_FDX (1 << 18)
#define VSC73XX_MAC_CFG_GIGE (1 << 17)
#define VSC73XX_MAC_CFG_RX_EN (1 << 16)
#define VSC73XX_MAC_CFG_VLAN_DBLAWR (1 << 15)
#define VSC73XX_MAC_CFG_VLAN_AWR (1 << 14)
#define VSC73XX_MAC_CFG_100_BASE_T (1 << 13)
#define VSC73XX_MAC_CFG_TX_IPG(x) (((x) & 0x1f) << 6)
#define VSC73XX_MAC_CFG_MAC_RX_RST (1 << 5)
#define VSC73XX_MAC_CFG_MAC_TX_RST (1 << 4)
#define VSC73XX_MAC_CFG_BIT2 (1 << 2)
#define VSC73XX_MAC_CFG_CLK_SEL(x) ((x) & 0x3)
/* ADVPORTM register bits */
#define VSC73XX_ADVPORTM_IFG_PPM (1 << 7)
#define VSC73XX_ADVPORTM_EXC_COL_CONT (1 << 6)
#define VSC73XX_ADVPORTM_EXT_PORT (1 << 5)
#define VSC73XX_ADVPORTM_INV_GTX (1 << 4)
#define VSC73XX_ADVPORTM_ENA_GTX (1 << 3)
#define VSC73XX_ADVPORTM_DDR_MODE (1 << 2)
#define VSC73XX_ADVPORTM_IO_LOOPBACK (1 << 1)
#define VSC73XX_ADVPORTM_HOST_LOOPBACK (1 << 0)
/* MII Block registers */
#define VSC73XX_MII_STAT 0x0
#define VSC73XX_MII_CMD 0x1
#define VSC73XX_MII_DATA 0x2
/* System Block registers */
#define VSC73XX_ICPU_SIPAD 0x01
#define VSC73XX_ICPU_CLOCK_DELAY 0x05
#define VSC73XX_ICPU_CTRL 0x10
#define VSC73XX_ICPU_ADDR 0x11
#define VSC73XX_ICPU_SRAM 0x12
#define VSC73XX_ICPU_MBOX_VAL 0x15
#define VSC73XX_ICPU_MBOX_SET 0x16
#define VSC73XX_ICPU_MBOX_CLR 0x17
#define VSC73XX_ICPU_CHIPID 0x18
#define VSC73XX_ICPU_GPIO 0x34
#define VSC73XX_ICPU_CTRL_CLK_DIV (1 << 8)
#define VSC73XX_ICPU_CTRL_SRST_HOLD (1 << 7)
#define VSC73XX_ICPU_CTRL_BOOT_EN (1 << 3)
#define VSC73XX_ICPU_CTRL_EXT_ACC_EN (1 << 2)
#define VSC73XX_ICPU_CTRL_CLK_EN (1 << 1)
#define VSC73XX_ICPU_CTRL_SRST (1 << 0)
#define VSC73XX_ICPU_CHIPID_ID_SHIFT 12
#define VSC73XX_ICPU_CHIPID_ID_MASK 0xffff
#define VSC73XX_ICPU_CHIPID_REV_SHIFT 28
#define VSC73XX_ICPU_CHIPID_REV_MASK 0xf
#define VSC73XX_ICPU_CHIPID_ID_7385 0x7385
#define VSC73XX_ICPU_CHIPID_ID_7395 0x7395
#define VSC73XX_CMD_MODE_READ 0
#define VSC73XX_CMD_MODE_WRITE 1
#define VSC73XX_CMD_MODE_SHIFT 4
#define VSC73XX_CMD_BLOCK_SHIFT 5
#define VSC73XX_CMD_BLOCK_MASK 0x7
#define VSC73XX_CMD_SUBBLOCK_MASK 0xf
#define VSC7385_CLOCK_DELAY ((3 << 4) | 3)
#define VSC7385_CLOCK_DELAY_MASK ((3 << 4) | 3)
#define VSC73XX_ICPU_CTRL_STOP (VSC73XX_ICPU_CTRL_SRST_HOLD | \
VSC73XX_ICPU_CTRL_BOOT_EN | \
VSC73XX_ICPU_CTRL_EXT_ACC_EN)
#define VSC73XX_ICPU_CTRL_START (VSC73XX_ICPU_CTRL_CLK_DIV | \
VSC73XX_ICPU_CTRL_BOOT_EN | \
VSC73XX_ICPU_CTRL_CLK_EN | \
VSC73XX_ICPU_CTRL_SRST)
#define VSC7385_ADVPORTM_MASK (VSC73XX_ADVPORTM_IFG_PPM | \
VSC73XX_ADVPORTM_EXC_COL_CONT | \
VSC73XX_ADVPORTM_EXT_PORT | \
VSC73XX_ADVPORTM_INV_GTX | \
VSC73XX_ADVPORTM_ENA_GTX | \
VSC73XX_ADVPORTM_DDR_MODE | \
VSC73XX_ADVPORTM_IO_LOOPBACK | \
VSC73XX_ADVPORTM_HOST_LOOPBACK)
#define VSC7385_ADVPORTM_INIT (VSC73XX_ADVPORTM_EXT_PORT | \
VSC73XX_ADVPORTM_ENA_GTX | \
VSC73XX_ADVPORTM_DDR_MODE)
#define VSC7385_MAC_CFG_RESET (VSC73XX_MAC_CFG_PORT_RST | \
VSC73XX_MAC_CFG_MAC_RX_RST | \
VSC73XX_MAC_CFG_MAC_TX_RST)
#define VSC73XX_MAC_CFG_INIT (VSC73XX_MAC_CFG_TX_EN | \
VSC73XX_MAC_CFG_FDX | \
VSC73XX_MAC_CFG_GIGE | \
VSC73XX_MAC_CFG_RX_EN)
#define VSC73XX_RESET_DELAY 100
struct vsc7385 {
struct spi_device *spi;
struct mutex lock;
struct vsc7385_platform_data *pdata;
};
static int vsc7385_is_addr_valid(u8 block, u8 subblock)
{
switch (block) {
case VSC73XX_BLOCK_MAC:
switch (subblock) {
case 0 ... 4:
case 6:
return 1;
}
break;
case VSC73XX_BLOCK_2:
case VSC73XX_BLOCK_SYSTEM:
switch (subblock) {
case 0:
return 1;
}
break;
case VSC73XX_BLOCK_MII:
case VSC73XX_BLOCK_4:
case VSC73XX_BLOCK_5:
switch (subblock) {
case 0 ... 1:
return 1;
}
break;
}
return 0;
}
static inline u8 vsc7385_make_addr(u8 mode, u8 block, u8 subblock)
{
u8 ret;
ret = (block & VSC73XX_CMD_BLOCK_MASK) << VSC73XX_CMD_BLOCK_SHIFT;
ret |= (mode & 1) << VSC73XX_CMD_MODE_SHIFT;
ret |= subblock & VSC73XX_CMD_SUBBLOCK_MASK;
return ret;
}
static int vsc7385_read(struct vsc7385 *vsc, u8 block, u8 subblock, u8 reg,
u32 *value)
{
u8 cmd[4];
u8 buf[4];
struct spi_transfer t[2];
struct spi_message m;
int err;
if (!vsc7385_is_addr_valid(block, subblock))
return -EINVAL;
spi_message_init(&m);
memset(&t, 0, sizeof(t));
t[0].tx_buf = cmd;
t[0].len = sizeof(cmd);
spi_message_add_tail(&t[0], &m);
t[1].rx_buf = buf;
t[1].len = sizeof(buf);
spi_message_add_tail(&t[1], &m);
cmd[0] = vsc7385_make_addr(VSC73XX_CMD_MODE_READ, block, subblock);
cmd[1] = reg;
cmd[2] = 0;
cmd[3] = 0;
mutex_lock(&vsc->lock);
err = spi_sync(vsc->spi, &m);
mutex_unlock(&vsc->lock);
if (err)
return err;
*value = (((u32) buf[0]) << 24) | (((u32) buf[1]) << 16) |
(((u32) buf[2]) << 8) | ((u32) buf[3]);
return 0;
}
static int vsc7385_write(struct vsc7385 *vsc, u8 block, u8 subblock, u8 reg,
u32 value)
{
u8 cmd[2];
u8 buf[4];
struct spi_transfer t[2];
struct spi_message m;
int err;
if (!vsc7385_is_addr_valid(block, subblock))
return -EINVAL;
spi_message_init(&m);
memset(&t, 0, sizeof(t));
t[0].tx_buf = cmd;
t[0].len = sizeof(cmd);
spi_message_add_tail(&t[0], &m);
t[1].tx_buf = buf;
t[1].len = sizeof(buf);
spi_message_add_tail(&t[1], &m);
cmd[0] = vsc7385_make_addr(VSC73XX_CMD_MODE_WRITE, block, subblock);
cmd[1] = reg;
buf[0] = (value >> 24) & 0xff;
buf[1] = (value >> 16) & 0xff;
buf[2] = (value >> 8) & 0xff;
buf[3] = value & 0xff;
mutex_lock(&vsc->lock);
err = spi_sync(vsc->spi, &m);
mutex_unlock(&vsc->lock);
return err;
}
static inline int vsc7385_write_verify(struct vsc7385 *vsc, u8 block,
u8 subblock, u8 reg, u32 value,
u32 read_mask, u32 read_val)
{
struct spi_device *spi = vsc->spi;
u32 t;
int err;
err = vsc7385_write(vsc, block, subblock, reg, value);
if (err)
return err;
err = vsc7385_read(vsc, block, subblock, reg, &t);
if (err)
return err;
if ((t & read_mask) != read_val) {
dev_err(&spi->dev, "register write error\n");
return -EIO;
}
return 0;
}
static inline int vsc7385_set_clock_delay(struct vsc7385 *vsc, u32 val)
{
return vsc7385_write(vsc, VSC73XX_BLOCK_SYSTEM, 0,
VSC73XX_ICPU_CLOCK_DELAY, val);
}
static inline int vsc7385_get_clock_delay(struct vsc7385 *vsc, u32 *val)
{
return vsc7385_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
VSC73XX_ICPU_CLOCK_DELAY, val);
}
static inline int vsc7385_icpu_stop(struct vsc7385 *vsc)
{
return vsc7385_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_ICPU_CTRL,
VSC73XX_ICPU_CTRL_STOP);
}
static inline int vsc7385_icpu_start(struct vsc7385 *vsc)
{
return vsc7385_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_ICPU_CTRL,
VSC73XX_ICPU_CTRL_START);
}
static inline int vsc7385_icpu_reset(struct vsc7385 *vsc)
{
int rc;
rc = vsc7385_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_ICPU_ADDR,
0x0000);
if (rc)
dev_err(&vsc->spi->dev,
"could not reset microcode, err=%d\n", rc);
return rc;
}
static int vsc7385_upload_ucode(struct vsc7385 *vsc)
{
struct spi_device *spi = vsc->spi;
const struct firmware *firmware;
char *ucode_name;
unsigned char *dp;
unsigned int curVal;
int i;
int diffs;
int rc;
ucode_name = (vsc->pdata->ucode_name) ? vsc->pdata->ucode_name
: "vsc7385_ucode.bin";
rc = request_firmware(&firmware, ucode_name, &spi->dev);
if (rc) {
dev_err(&spi->dev, "request_firmware failed, err=%d\n",
rc);
return rc;
}
rc = vsc7385_icpu_stop(vsc);
if (rc)
goto out;
rc = vsc7385_icpu_reset(vsc);
if (rc)
goto out;
dev_info(&spi->dev, "uploading microcode...\n");
dp = (unsigned char *) firmware->data;
for (i = 0; i < firmware->size; i++) {
rc = vsc7385_write(vsc, VSC73XX_BLOCK_SYSTEM, 0,
VSC73XX_ICPU_SRAM, *dp++);
if (rc) {
dev_err(&spi->dev, "could not load microcode, err=%d\n",
rc);
goto out;
}
}
rc = vsc7385_icpu_reset(vsc);
if (rc)
goto out;
dev_info(&spi->dev, "verifying microcode...\n");
dp = (unsigned char *) firmware->data;
diffs = 0;
for (i = 0; i < firmware->size; i++) {
rc = vsc7385_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
VSC73XX_ICPU_SRAM, &curVal);
if (rc) {
dev_err(&spi->dev, "could not read microcode %d\n",
rc);
goto out;
}
if (curVal > 0xff) {
dev_err(&spi->dev, "bad val read: %04x : %02x %02x\n",
i, *dp, curVal);
rc = -EIO;
goto out;
}
if ((curVal & 0xff) != *dp) {
diffs++;
dev_err(&spi->dev, "verify error: %04x : %02x %02x\n",
i, *dp, curVal);
if (diffs > 4)
break;
}
dp++;
}
if (diffs) {
dev_err(&spi->dev, "microcode verification failed\n");
rc = -EIO;
goto out;
}
dev_info(&spi->dev, "microcode uploaded\n");
rc = vsc7385_icpu_start(vsc);
out:
release_firmware(firmware);
return rc;
}
static int vsc7385_setup(struct vsc7385 *vsc)
{
struct vsc7385_platform_data *pdata = vsc->pdata;
u32 t;
int err;
err = vsc7385_write_verify(vsc, VSC73XX_BLOCK_SYSTEM, 0,
VSC73XX_ICPU_CLOCK_DELAY,
VSC7385_CLOCK_DELAY,
VSC7385_CLOCK_DELAY_MASK,
VSC7385_CLOCK_DELAY);
if (err)
goto err;
err = vsc7385_write_verify(vsc, VSC73XX_BLOCK_MAC,
VSC73XX_SUBBLOCK_PORT_MAC, VSC73XX_ADVPORTM,
VSC7385_ADVPORTM_INIT,
VSC7385_ADVPORTM_MASK,
VSC7385_ADVPORTM_INIT);
if (err)
goto err;
err = vsc7385_write(vsc, VSC73XX_BLOCK_MAC, VSC73XX_SUBBLOCK_PORT_MAC,
VSC73XX_MAC_CFG, VSC7385_MAC_CFG_RESET);
if (err)
goto err;
t = VSC73XX_MAC_CFG_INIT;
t |= VSC73XX_MAC_CFG_TX_IPG(pdata->mac_cfg.tx_ipg);
t |= VSC73XX_MAC_CFG_CLK_SEL(pdata->mac_cfg.clk_sel);
if (pdata->mac_cfg.bit2)
t |= VSC73XX_MAC_CFG_BIT2;
err = vsc7385_write(vsc, VSC73XX_BLOCK_MAC, VSC73XX_SUBBLOCK_PORT_MAC,
VSC73XX_MAC_CFG, t);
if (err)
goto err;
return 0;
err:
return err;
}
static int vsc7385_detect(struct vsc7385 *vsc)
{
struct spi_device *spi = vsc->spi;
u32 t;
u32 id;
u32 rev;
int err;
err = vsc7385_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
VSC73XX_ICPU_MBOX_VAL, &t);
if (err) {
dev_err(&spi->dev, "unable to read mailbox, err=%d\n", err);
return err;
}
if (t == 0xffffffff) {
dev_dbg(&spi->dev, "assert chip reset\n");
if (vsc->pdata->reset)
vsc->pdata->reset();
}
err = vsc7385_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
VSC73XX_ICPU_CHIPID, &t);
if (err) {
dev_err(&spi->dev, "unable to read chip id, err=%d\n", err);
return err;
}
id = (t >> VSC73XX_ICPU_CHIPID_ID_SHIFT) & VSC73XX_ICPU_CHIPID_ID_MASK;
switch (id) {
case VSC73XX_ICPU_CHIPID_ID_7385:
case VSC73XX_ICPU_CHIPID_ID_7395:
break;
default:
dev_err(&spi->dev, "unsupported chip, id=%04x\n", id);
return -ENODEV;
}
rev = (t >> VSC73XX_ICPU_CHIPID_REV_SHIFT) &
VSC73XX_ICPU_CHIPID_REV_MASK;
dev_info(&spi->dev, "VSC%04X (rev. %d) switch found\n", id, rev);
return 0;
}
static int vsc7385_probe(struct spi_device *spi)
{
struct vsc7385 *vsc;
struct vsc7385_platform_data *pdata;
int err;
printk(KERN_INFO DRV_DESC " version " DRV_VERSION"\n");
pdata = spi->dev.platform_data;
if (!pdata) {
dev_err(&spi->dev, "no platform data specified\n");
return -ENODEV;
}
vsc = kzalloc(sizeof(*vsc), GFP_KERNEL);
if (!vsc) {
dev_err(&spi->dev, "no memory for private data\n");
return -ENOMEM;
}
mutex_init(&vsc->lock);
vsc->pdata = pdata;
vsc->spi = spi_dev_get(spi);
dev_set_drvdata(&spi->dev, vsc);
spi->mode = SPI_MODE_0;
spi->bits_per_word = 8;
err = spi_setup(spi);
if (err) {
dev_err(&spi->dev, "spi_setup failed, err=%d\n", err);
goto err_drvdata;
}
err = vsc7385_detect(vsc);
if (err) {
dev_err(&spi->dev, "no chip found, err=%d\n", err);
goto err_drvdata;
}
err = vsc7385_upload_ucode(vsc);
if (err)
goto err_drvdata;
err = vsc7385_setup(vsc);
if (err)
goto err_drvdata;
return 0;
err_drvdata:
dev_set_drvdata(&spi->dev, NULL);
kfree(vsc);
return err;
}
static int vsc7385_remove(struct spi_device *spi)
{
struct vsc7385_data *vsc;
vsc = dev_get_drvdata(&spi->dev);
dev_set_drvdata(&spi->dev, NULL);
kfree(vsc);
return 0;
}
static struct spi_driver vsc7385_driver = {
.driver = {
.name = DRV_NAME,
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
.probe = vsc7385_probe,
.remove = vsc7385_remove,
};
static int __init vsc7385_init(void)
{
return spi_register_driver(&vsc7385_driver);
}
module_init(vsc7385_init);
static void __exit vsc7385_exit(void)
{
spi_unregister_driver(&vsc7385_driver);
}
module_exit(vsc7385_exit);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");