Fresh pull from upstream
[librecmc/librecmc.git] / target / linux / ar71xx / files / arch / mips / ath79 / dev-m25p80.c
1 /*
2  *  Copyright (C) 2009-2012 Gabor Juhos <juhosg@openwrt.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU General Public License version 2 as published
6  *  by the Free Software Foundation.
7  */
8
9 #include <linux/init.h>
10 #include <linux/spi/spi.h>
11 #include <linux/spi/flash.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/mtd/partitions.h>
14 #include <linux/mtd/concat.h>
15
16 #include "dev-spi.h"
17 #include "dev-m25p80.h"
18
19 static struct spi_board_info ath79_spi_info[] = {
20         {
21                 .bus_num        = 0,
22                 .chip_select    = 0,
23                 .max_speed_hz   = 25000000,
24                 .modalias       = "m25p80",
25         },
26         {
27                 .bus_num        = 0,
28                 .chip_select    = 1,
29                 .max_speed_hz   = 25000000,
30                 .modalias       = "m25p80",
31         }
32 };
33
34 static struct ath79_spi_platform_data ath79_spi_data;
35
36 void __init ath79_register_m25p80(struct flash_platform_data *pdata)
37 {
38         ath79_spi_data.bus_num = 0;
39         ath79_spi_data.num_chipselect = 1;
40         ath79_spi_info[0].platform_data = pdata;
41         ath79_register_spi(&ath79_spi_data, ath79_spi_info, 1);
42 }
43
44 static struct flash_platform_data *multi_pdata;
45
46 static struct mtd_info *concat_devs[2] = { NULL, NULL };
47 static struct work_struct mtd_concat_work;
48
49 static void mtd_concat_add_work(struct work_struct *work)
50 {
51         struct mtd_info *mtd;
52
53         mtd = mtd_concat_create(concat_devs, ARRAY_SIZE(concat_devs), "flash");
54
55         mtd_device_register(mtd, multi_pdata->parts, multi_pdata->nr_parts);
56 }
57
58 static void mtd_concat_add(struct mtd_info *mtd)
59 {
60         static bool registered = false;
61
62         if (registered)
63                 return;
64
65         if (!strcmp(mtd->name, "spi0.0"))
66                 concat_devs[0] = mtd;
67         else if (!strcmp(mtd->name, "spi0.1"))
68                 concat_devs[1] = mtd;
69         else
70                 return;
71
72         if (!concat_devs[0] || !concat_devs[1])
73                 return;
74
75         registered = true;
76         INIT_WORK(&mtd_concat_work, mtd_concat_add_work);
77         schedule_work(&mtd_concat_work);
78 }
79
80 static void mtd_concat_remove(struct mtd_info *mtd)
81 {
82 }
83
84 static void add_mtd_concat_notifier(void)
85 {
86         static struct mtd_notifier not = {
87                 .add = mtd_concat_add,
88                 .remove = mtd_concat_remove,
89         };
90
91         register_mtd_user(&not);
92 }
93
94 void __init ath79_register_m25p80_multi(struct flash_platform_data *pdata)
95 {
96         multi_pdata = pdata;
97         add_mtd_concat_notifier();
98         ath79_spi_data.bus_num = 0;
99         ath79_spi_data.num_chipselect = 2;
100         ath79_register_spi(&ath79_spi_data, ath79_spi_info, 2);
101 }