* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
@ 2011-11-22 14:41 Robert Deliën
2011-11-22 14:49 ` Marek Vasut
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: Robert Deliën @ 2011-11-22 14:41 UTC (permalink / raw)
To: u-boot
This patch fixes a small bug that allowed unintended manipulation of non-existing GPIO pins within a pin bank, clobbering reserved bits.
Signed-off-by: Robert Deli?n <robert@delien.nl>
diff --git a/arch/arm/include/asm/arch-mx28/iomux.h b/arch/arm/include/asm/arch-mx28/iomux.h
index 7abdf58..829d9a8 100644
--- a/arch/arm/include/asm/arch-mx28/iomux.h
+++ b/arch/arm/include/asm/arch-mx28/iomux.h
@@ -56,6 +56,12 @@ typedef u32 iomux_cfg_t;
#define MXS_PAD_PULL_VALID_SHIFT 16
#define MXS_PAD_PULL_VALID_MASK ((iomux_cfg_t)0x1 << MXS_PAD_PULL_VALID_SHIFT)
+#define MXS_BANK0_PINS 29
+#define MXS_BANK1_PINS 32
+#define MXS_BANK2_PINS 28
+#define MXS_BANK3_PINS 31
+#define MXS_BANK4_PINS 21
+
#define PAD_MUXSEL_0 0
#define PAD_MUXSEL_1 1
#define PAD_MUXSEL_2 2
diff --git a/drivers/gpio/mxs_gpio.c b/drivers/gpio/mxs_gpio.c
index 539738b..fbc6da3 100644
--- a/drivers/gpio/mxs_gpio.c
+++ b/drivers/gpio/mxs_gpio.c
@@ -120,9 +120,34 @@ int gpio_direction_output(int gp, int value)
int gpio_request(int gp, const char *label)
{
+ int bank_pins;
+
if (PAD_BANK(gp) >= PINCTRL_BANKS)
return -EINVAL;
+ switch(PAD_BANK(gp)) {
+ case 0:
+ bank_pins = MXS_BANK0_PINS;
+ break;
+ case 1:
+ bank_pins = MXS_BANK1_PINS;
+ break;
+ case 2:
+ bank_pins = MXS_BANK2_PINS;
+ break;
+ case 3:
+ bank_pins = MXS_BANK3_PINS;
+ break;
+ case 4:
+ bank_pins = MXS_BANK4_PINS;
+ break;
+ default:
+ bank_pins = 0;
+ }
+
+ if (PAD_PIN(gp) >= bank_pins)
+ return -EINVAL;
+
return 0;
}
^ permalink raw reply related [flat|nested] 13+ messages in thread* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-22 14:41 [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver Robert Deliën
@ 2011-11-22 14:49 ` Marek Vasut
[not found] ` <B4232BD0-3457-4DC5-9A4A-4E16719BA157@delien.nl>
2011-11-22 18:20 ` Marek Vasut
2011-11-22 21:09 ` Mike Frysinger
2 siblings, 1 reply; 13+ messages in thread
From: Marek Vasut @ 2011-11-22 14:49 UTC (permalink / raw)
To: u-boot
> This patch fixes a small bug that allowed unintended manipulation of
> non-existing GPIO pins within a pin bank, clobbering reserved bits.
>
> Signed-off-by: Robert Deli?n <robert@delien.nl>
> diff --git a/arch/arm/include/asm/arch-mx28/iomux.h
> b/arch/arm/include/asm/arch-mx28/iomux.h index 7abdf58..829d9a8 100644
> --- a/arch/arm/include/asm/arch-mx28/iomux.h
> +++ b/arch/arm/include/asm/arch-mx28/iomux.h
> @@ -56,6 +56,12 @@ typedef u32 iomux_cfg_t;
> #define MXS_PAD_PULL_VALID_SHIFT 16
> #define MXS_PAD_PULL_VALID_MASK ((iomux_cfg_t)0x1 <<
> MXS_PAD_PULL_VALID_SHIFT)
>
> +#define MXS_BANK0_PINS 29
> +#define MXS_BANK1_PINS 32
> +#define MXS_BANK2_PINS 28
> +#define MXS_BANK3_PINS 31
> +#define MXS_BANK4_PINS 21
> +
> #define PAD_MUXSEL_0 0
> #define PAD_MUXSEL_1 1
> #define PAD_MUXSEL_2 2
> diff --git a/drivers/gpio/mxs_gpio.c b/drivers/gpio/mxs_gpio.c
> index 539738b..fbc6da3 100644
> --- a/drivers/gpio/mxs_gpio.c
> +++ b/drivers/gpio/mxs_gpio.c
> @@ -120,9 +120,34 @@ int gpio_direction_output(int gp, int value)
>
> int gpio_request(int gp, const char *label)
> {
> + int bank_pins;
> +
> if (PAD_BANK(gp) >= PINCTRL_BANKS)
> return -EINVAL;
>
> + switch(PAD_BANK(gp)) {
> + case 0:
> + bank_pins = MXS_BANK0_PINS;
> + break;
> + case 1:
> + bank_pins = MXS_BANK1_PINS;
> + break;
> + case 2:
> + bank_pins = MXS_BANK2_PINS;
> + break;
> + case 3:
> + bank_pins = MXS_BANK3_PINS;
> + break;
> + case 4:
> + bank_pins = MXS_BANK4_PINS;
> + break;
> + default:
> + bank_pins = 0;
> + }
> +
> + if (PAD_PIN(gp) >= bank_pins)
> + return -EINVAL;
> +
> return 0;
> }
Hey, this looks reasonable. Did you send similar patch to Linux too ?
M
^ permalink raw reply [flat|nested] 13+ messages in thread* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-22 14:41 [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver Robert Deliën
2011-11-22 14:49 ` Marek Vasut
@ 2011-11-22 18:20 ` Marek Vasut
2011-11-22 19:24 ` Robert Deliën
2011-11-23 10:47 ` Robert Deliën
2011-11-22 21:09 ` Mike Frysinger
2 siblings, 2 replies; 13+ messages in thread
From: Marek Vasut @ 2011-11-22 18:20 UTC (permalink / raw)
To: u-boot
> This patch fixes a small bug that allowed unintended manipulation of
> non-existing GPIO pins within a pin bank, clobbering reserved bits.
>
> Signed-off-by: Robert Deli?n <robert@delien.nl>
> diff --git a/arch/arm/include/asm/arch-mx28/iomux.h
> b/arch/arm/include/asm/arch-mx28/iomux.h index 7abdf58..829d9a8 100644
> --- a/arch/arm/include/asm/arch-mx28/iomux.h
> +++ b/arch/arm/include/asm/arch-mx28/iomux.h
> @@ -56,6 +56,12 @@ typedef u32 iomux_cfg_t;
> #define MXS_PAD_PULL_VALID_SHIFT 16
> #define MXS_PAD_PULL_VALID_MASK ((iomux_cfg_t)0x1 <<
> MXS_PAD_PULL_VALID_SHIFT)
>
> +#define MXS_BANK0_PINS 29
> +#define MXS_BANK1_PINS 32
> +#define MXS_BANK2_PINS 28
> +#define MXS_BANK3_PINS 31
> +#define MXS_BANK4_PINS 21
> +
> #define PAD_MUXSEL_0 0
> #define PAD_MUXSEL_1 1
> #define PAD_MUXSEL_2 2
> diff --git a/drivers/gpio/mxs_gpio.c b/drivers/gpio/mxs_gpio.c
> index 539738b..fbc6da3 100644
> --- a/drivers/gpio/mxs_gpio.c
> +++ b/drivers/gpio/mxs_gpio.c
> @@ -120,9 +120,34 @@ int gpio_direction_output(int gp, int value)
>
> int gpio_request(int gp, const char *label)
> {
> + int bank_pins;
> +
> if (PAD_BANK(gp) >= PINCTRL_BANKS)
> return -EINVAL;
>
> + switch(PAD_BANK(gp)) {
> + case 0:
> + bank_pins = MXS_BANK0_PINS;
> + break;
> + case 1:
> + bank_pins = MXS_BANK1_PINS;
> + break;
> + case 2:
> + bank_pins = MXS_BANK2_PINS;
> + break;
> + case 3:
> + bank_pins = MXS_BANK3_PINS;
> + break;
> + case 4:
> + bank_pins = MXS_BANK4_PINS;
> + break;
> + default:
> + bank_pins = 0;
> + }
> +
> + if (PAD_PIN(gp) >= bank_pins)
> + return -EINVAL;
> +
> return 0;
> }
Careful here !!
The driver _should_ work for MX233 too! What I'd like to see is you introducing
a function like:
int mxs_gpio_is_valid(gpio)
{
char mxs_banks[PINCTRL_BANKS] = PINCTRL_BANK_COUNTS;
if (PAD_PIN(gpio) > mxs_bank[PAD_BANK(gpio)])
return -EINVAL;
return 0;
}
And define PINCTRL_BANK_COUNTS in the section of mxs_gpio.c where all the
remaining mx28 and mx233 specific defines are hoarded (near the top of the
file).
M
^ permalink raw reply [flat|nested] 13+ messages in thread* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-22 18:20 ` Marek Vasut
@ 2011-11-22 19:24 ` Robert Deliën
2011-11-22 22:36 ` Marek Vasut
2011-11-23 10:47 ` Robert Deliën
1 sibling, 1 reply; 13+ messages in thread
From: Robert Deliën @ 2011-11-22 19:24 UTC (permalink / raw)
To: u-boot
> Careful here !!
>
> The driver _should_ work for MX233 too! What I'd like to see is you introducing
> a function like:
>
> int mxs_gpio_is_valid(gpio)
> {
> char mxs_banks[PINCTRL_BANKS] = PINCTRL_BANK_COUNTS;
>
> if (PAD_PIN(gpio) > mxs_bank[PAD_BANK(gpio)])
> return -EINVAL;
>
> return 0;
> }
>
> And define PINCTRL_BANK_COUNTS in the section of mxs_gpio.c where all the
> remaining mx28 and mx233 specific defines are hoarded (near the top of the
> file).
Does mx233 use arch/arm/include/asm/arch-mx28/iomux.h? In that case we should consider renaming it to arch/arm/include/asm/arch-mxs, for all Sigmatel base FreeScale SoCs.
I do like the idea of a function validating pins. If I'm going to introduce changes that big, I may also introduce functionality to set pin functions run-time, as that is very useful during board bring-up.
^ permalink raw reply [flat|nested] 13+ messages in thread* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-22 19:24 ` Robert Deliën
@ 2011-11-22 22:36 ` Marek Vasut
0 siblings, 0 replies; 13+ messages in thread
From: Marek Vasut @ 2011-11-22 22:36 UTC (permalink / raw)
To: u-boot
> > Careful here !!
> >
> > The driver _should_ work for MX233 too! What I'd like to see is you
> > introducing a function like:
> >
> > int mxs_gpio_is_valid(gpio)
> > {
> > char mxs_banks[PINCTRL_BANKS] = PINCTRL_BANK_COUNTS;
> >
> > if (PAD_PIN(gpio) > mxs_bank[PAD_BANK(gpio)])
> >
> > return -EINVAL;
> >
> > return 0;
> > }
> >
> > And define PINCTRL_BANK_COUNTS in the section of mxs_gpio.c where all the
> > remaining mx28 and mx233 specific defines are hoarded (near the top of
> > the file).
>
> Does mx233 use arch/arm/include/asm/arch-mx28/iomux.h? In that case we
> should consider renaming it to arch/arm/include/asm/arch-mxs, for all
> Sigmatel base FreeScale SoCs.
It doesn't, because it's not yet supported.
>
> I do like the idea of a function validating pins. If I'm going to introduce
> changes that big, I may also introduce functionality to set pin functions
> run-time, as that is very useful during board bring-up.
Please introduce the validation function first, the runtime stuff can be done
later.
M
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-22 18:20 ` Marek Vasut
2011-11-22 19:24 ` Robert Deliën
@ 2011-11-23 10:47 ` Robert Deliën
2011-11-23 10:59 ` Marek Vasut
1 sibling, 1 reply; 13+ messages in thread
From: Robert Deliën @ 2011-11-23 10:47 UTC (permalink / raw)
To: u-boot
> Careful here !!
>
> The driver _should_ work for MX233 too! What I'd like to see is you introducing
> a function like:
>
> int mxs_gpio_is_valid(gpio)
> {
> char mxs_banks[PINCTRL_BANKS] = PINCTRL_BANK_COUNTS;
>
> if (PAD_PIN(gpio) > mxs_bank[PAD_BANK(gpio)])
> return -EINVAL;
>
> return 0;
> }
There's a bit of a paradox here: If a name is translated into a pin, bank numbers above 7 and pin numbers above 31 will have wrapped around in translation and won't be caught here. I could check for wrapping in the translating function and check for valid numbers within the assigned bit range here, but I'd rather not see validity check spread over two functions.
> And define PINCTRL_BANK_COUNTS in the section of mxs_gpio.c where all the
> remaining mx28 and mx233 specific defines are hoarded (near the top of the
> file).
At that spot I've put in Mike's (very similar) solution now:
static const int mxs_bank_pins[] = {
MXS_BANK0_PINS,
MXS_BANK1_PINS,
MXS_BANK2_PINS,
#ifdef(CONFIG_MX28)
MXS_BANK3_PINS,
MXS_BANK4_PINS,
#endif
};
I'm considering to remove the macro PINCTRL_BANKS now, and use ARRAY_SIZE(mxs_bank_pins) instead, as it yields the same number and leads to a single point of definition.
^ permalink raw reply [flat|nested] 13+ messages in thread* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-23 10:47 ` Robert Deliën
@ 2011-11-23 10:59 ` Marek Vasut
2011-11-23 12:36 ` Robert Deliën
0 siblings, 1 reply; 13+ messages in thread
From: Marek Vasut @ 2011-11-23 10:59 UTC (permalink / raw)
To: u-boot
> > Careful here !!
> >
> > The driver _should_ work for MX233 too! What I'd like to see is you
> > introducing a function like:
> >
> > int mxs_gpio_is_valid(gpio)
> > {
> > char mxs_banks[PINCTRL_BANKS] = PINCTRL_BANK_COUNTS;
> >
> > if (PAD_PIN(gpio) > mxs_bank[PAD_BANK(gpio)])
> > return -EINVAL;
> >
> > return 0;
> > }
>
> There's a bit of a paradox here: If a name is translated into a pin, bank
> numbers above 7 and pin numbers above 31 will have wrapped around in
> translation and won't be caught here. I could check for wrapping in the
> translating function and check for valid numbers within the assigned bit
> range here, but I'd rather not see validity check spread over two
> functions.
>
> > And define PINCTRL_BANK_COUNTS in the section of mxs_gpio.c where all the
> > remaining mx28 and mx233 specific defines are hoarded (near the top of
> > the file).
>
> At that spot I've put in Mike's (very similar) solution now:
> static const int mxs_bank_pins[] = {
> MXS_BANK0_PINS,
> MXS_BANK1_PINS,
> MXS_BANK2_PINS,
> #ifdef(CONFIG_MX28)
> MXS_BANK3_PINS,
> MXS_BANK4_PINS,
> #endif
> };
>
> I'm considering to remove the macro PINCTRL_BANKS now, and use
> ARRAY_SIZE(mxs_bank_pins) instead, as it yields the same number and leads
> to a single point of definition.
And are you sure the amound of pins in bank 0, 1, 2 is the same on mx233 and
mx28 ?
^ permalink raw reply [flat|nested] 13+ messages in thread* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-23 10:59 ` Marek Vasut
@ 2011-11-23 12:36 ` Robert Deliën
0 siblings, 0 replies; 13+ messages in thread
From: Robert Deliën @ 2011-11-23 12:36 UTC (permalink / raw)
To: u-boot
And are you sure the amound of pins in bank 0, 1, 2 is the same on mx233 and
mx28 ?
Yes, I'm sure they're not ;-). I went through the manual and changed it into this:
static const int mxs_bank_pins[] = {
#if defined(CONFIG_MX23)
MX23_BANK0_PINS,
MX23_BANK1_PINS,
MX23_BANK2_PINS
#elif defined(CONFIG_MX28)
MX28_BANK0_PINS,
MX28_BANK1_PINS,
MX28_BANK2_PINS,
MX28_BANK3_PINS,
MX28_BANK4_PINS
#endif
};
I also implemented a non-static function gpio_name_to_pin. I didn't implement validity checking in it, but is does check for set bits outside the pin and bank masks. To check if a pin is actually valid, I have added a non-static function gpio_invalid, as you suggested. I just named it differently because a function called gpio_is_valid returning 0 in case the pin is valid might be a bit confusing when evaluating the return parameter.
As a result, there's no longer a validity check in gpio_request anymore. Or would you suggest to do this check at the beginning of each non-static function taking a gp input parameter?
I'm not really sure yet how to implement Mike's suggestion to deal with diversity, but I'll work on that now.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver
2011-11-22 14:41 [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver Robert Deliën
2011-11-22 14:49 ` Marek Vasut
2011-11-22 18:20 ` Marek Vasut
@ 2011-11-22 21:09 ` Mike Frysinger
2011-11-22 21:16 ` Robert Deliën
2 siblings, 1 reply; 13+ messages in thread
From: Mike Frysinger @ 2011-11-22 21:09 UTC (permalink / raw)
To: u-boot
On Tuesday 22 November 2011 09:41:48 Robert Deli?n wrote:
> + switch(PAD_BANK(gp)) {
needs a space after that "switch"
> + case 0:
> + bank_pins = MXS_BANK0_PINS;
> + break;
> + case 1:
> + bank_pins = MXS_BANK1_PINS;
> + break;
> + case 2:
> + bank_pins = MXS_BANK2_PINS;
> + break;
> + case 3:
> + bank_pins = MXS_BANK3_PINS;
> + break;
> + case 4:
> + bank_pins = MXS_BANK4_PINS;
> + break;
> + default:
> + bank_pins = 0;
> + }
static const int all_bank_pins[] = {
MXS_BANK0_PINS,
MXS_BANK1_PINS,
MXS_BANK2_PINS,
MXS_BANK3_PINS,
MXS_BANK4_PINS,
};
bank_pins = all_bank_pins[PAD_PANK(gp)];
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20111122/9f361726/attachment.pgp>
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2011-11-23 12:36 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-22 14:41 [U-Boot] [PATCH] M28: Added guarding for reserved bits in GPIO driver Robert Deliën
2011-11-22 14:49 ` Marek Vasut
[not found] ` <B4232BD0-3457-4DC5-9A4A-4E16719BA157@delien.nl>
2011-11-22 15:25 ` Marek Vasut
2011-11-22 16:48 ` Robert Deliën
2011-11-22 18:08 ` Marek Vasut
2011-11-22 18:20 ` Marek Vasut
2011-11-22 19:24 ` Robert Deliën
2011-11-22 22:36 ` Marek Vasut
2011-11-23 10:47 ` Robert Deliën
2011-11-23 10:59 ` Marek Vasut
2011-11-23 12:36 ` Robert Deliën
2011-11-22 21:09 ` Mike Frysinger
2011-11-22 21:16 ` Robert Deliën
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox